在c#中显示图像

ha.*_*.ed 16 c# winforms

我想用c#显示图像PictureBox.我创建了一个包含a pictureBox和计时器的类.但是当创建对象时,没有任何显示.

我该怎么办?

我正确使用timer1吗?

这是我的代码:

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        c1 c = new c1();
        c.create_move(1);
    }

}

class c1 {

    PictureBox p = new PictureBox();
    Timer timer1 = new Timer();

    public void create_move(int i){

        p.ImageLocation = "1.png";
        p.Location = new Point(50, 50 + (i - 1) * 50);

        timer1.Start();
        timer1.Interval = 15;
        timer1.Tick += new EventHandler(timer_Tick);
    }


    private int k = 0;
    void timer_Tick(object sender, EventArgs e)
    {
         // some code. this part work outside the class c1 properly.
         ...

    }
Run Code Online (Sandbox Code Playgroud)

Dar*_*mas 19

您需要将图片框添加到a Form.看看Form.Controls.Add()方法.


Shi*_*rod 7

这是因为您的图片框未添加到当前表单中.

你有一个Form.Controls属性,它有一个Add()方法.