我正在创建一个带有两个pictureBox的应用程序,其中图像从数组加载并随机更改每个计时器滴答。我面临的问题是有时候没有加载图像,让图片框有一点红叉。我读过我应该在加载新图片之前处理上一张图片(我是C#newbie),但是如果没有在第一个刻度线上崩溃应用程序我就无法工作。你能帮帮我吗?
private void timer_Tick(object sender, EventArgs e)
{
index1 = rand.Next(0, pics.Length - 1);
index2 = rand.Next(0, pics.Length - 1);
pcb1.ImageLocation = pics[index1];
pcb2.ImageLocation = pics[index2];
}
try
{
pcb1.Image.Dispose();
pcb2.Image.Dispose();
index1 = rand.Next(0, pics.Length - 1);
index2 = rand.Next(0, pics.Length - 1);
pcb1.ImageLocation = pics[index1];
pcb2.ImageLocation = pics[index2];
}
catch (Exception)
{
throw;
}
- 告诉我System.ArgumentException
答案 0 :(得分:0)
可能的解决方案:
try
{
private void timer_Tick(object sender, EventArgs e)
{
index1 = rand.Next(0, pics.Length);
if (File.Exists(pics[index1]))
{
Image img1 = Image.FromFile(pics[index1]);
pcb1.Image = img1;
}
index2 = rand.Next(0, pics.Length);
if (File.Exists(pics[index2]))
{
Image img2 = Image.FromFile(pics[index2]);
pcb2.Image = img2;
}
}
}
catch (OutOfMemoryException oomEx)
{
MessageBox.Show("Not a valid image.");
}
catch (Exception ex)
{
//all others...respond appropriately
}