问题
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