嗨我有一个小问题我创建了一个计时器来显示6张图片它循环通过图片盒1和2中的图片但是由于某种原因它不显示任何图片只是一个x标记。
我的图片格式正确,但我不确定问题是什么,任何人都可以帮忙吗?
string[] picture = {
@"ImageResource\Die_Images\die1.jpg",
@"ImageResource\Die_Images\die2.jpg",
@"ImageResource\Die_Images\die3.jpg",
@"ImageResource\Die_Images\die4.jpg",
@"ImageResource\Die_Images\die5.jpg",
@"ImageResource\Die_Images\die6.jpg"};
int index = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox2.ImageLocation = string.Format(@"ImageResource\Die_Images\{0}.jpg", index);
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
index++;
if (index >= picture.Length)
index = 0;
else
pictureBox1.ImageLocation = string.Format(@"ImageResource\Die_Images\{0}.jpg", index);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
答案 0 :(得分:1)
这应该
pictureBox2.ImageLocation = string.Format(@"ImageResource\Die_Images\{0}.jpg", index);
是吗?
pictureBox2.ImageLocation = string.Format(@"ImageResource\Die_Images\die{0}.jpg", index);
答案 1 :(得分:1)
您需要"die"+index
string.Format(@"ImageResource\Die_Images\{0}.jpg", "die"+index);
^^^
您的图片名称为die1.jpg
,die2.jpg
...但您尝试显示1.jpg
,2.jpg
等等...此外,您应该开始索引来自1
而不是0
。