动态更改图片框中的图像

时间:2013-12-17 21:02:06

标签: c# dynamic picturebox

我想运行一段代码,不断更改图片框(如旋转螺旋桨)的图片,直到表格关闭。

我设法用EventHandler改变了PictureBox的图片,但我不知道该怎么做。

    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(pb);
    }

    PictureBox pb = new PictureBox
    {
        Location = new Point(0, 0),
        SizeMode = PictureBoxSizeMode.Zoom,
        Size = new Size(300,300),
        ImageLocation = @"E:\folder\gas_jo.png"
    };

    private void Form1_Click(object sender, EventArgs e)
    {
        if (pb.ImageLocation == @"E:\folder\gas_jo.png")
        {
            pb.ImageLocation =@"E:\folder\gas_jo_1.png";
        }
        else if (pb.ImageLocation == @"E:\folder\gas_jo_1.png")
        {
            pb.ImageLocation = @"E:\folder\gas_jo.png";
        }
    }

2 个答案:

答案 0 :(得分:2)

System.Windows.Forms.Timer timer;

public Form1()
{
    InitializeComponent();
    this.Controls.Add(pb);

    timer = new System.Windows.Forms.Timer();
    timer.Interval = 1000;
    timer.Tick += (sender, args) => {
        if (pb.ImageLocation == @"E:\folder\gas_jo.png")
        {
            pb.ImageLocation =@"E:\folder\gas_jo_1.png";
        }
        else if (pb.ImageLocation == @"E:\Real-time_Imi\gas_jo_1.png")
        {
            pb.ImageLocation = @"E:\Real-time_Imi\gas_jo.png";
        }
    };
    timer.Start();
}

PictureBox pb = new PictureBox
{
    Location = new Point(0, 0),
    SizeMode = PictureBoxSizeMode.Zoom,
    Size = new Size(300,300),
    ImageLocation = @"E:\folder\gas_jo.png"
};

答案 1 :(得分:0)

string[] pictureBoxArray = new string[] { @"E:\folder\gas_jo.png", @"E:\folder\gas_jo_1.png", @"E:\folder\gas_jo_2.png" };

    int pctIndex = 0;  

    private void timer1_Tick(object sender, EventArgs e)
            {


                 pb.ImageLocation = pictureBoxArray[pctIndex]; 
                    pctIndex ++;  
                    if(pctIndex==pictureBoxArray.Length)
                        pctIndex =0 ;  
            }