Dynamically changing image in a picturebox

梦想与她 提交于 2019-12-11 19:43:38

问题


I'd like to run a piece of code which keeps changing the picture of a picturebox (like a rotating propeller) until the form is closed.

I managed to change the picture of the PictureBox with an EventHandler, but I don't know how to go along.

    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";
        }
    }

回答1:


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"
};



回答2:


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 ;  
            }


来源:https://stackoverflow.com/questions/20644589/dynamically-changing-image-in-a-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!