我正在使用此代码在 3 个图片框中以一定间隔随机显示来自 Image 文件夹的图像,但是ARandomNumber
我得到了,因此我得到了System.drawing.Bitmap
,因为我得到了FileNotFoundException
这条路径pictureBox3.image
C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\System.Drawing.Bitmap.png
我不明白我哪里出错了:
namespace StudentModule
{
public partial class Form1 : Form
{
Random r = new Random();
int index = -1;
List<Image> images;
Image ARandomNumber;
Timer timer = new Timer();
private int counter = 0;
public Form1()
{
InitializeComponent();
timer1.Interval = 350;
timer1.Tick += new EventHandler(timer1_Tick);
List<Image> images = new List<Image>();//add images to this array
DirectoryInfo di = new DirectoryInfo(@"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image"); // give path
FileInfo[] finfos = di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
foreach (FileInfo fi in finfos)
images.Add(Image.FromFile(fi.FullName));
index++;
if (index < 0 || index >= images.Count)
index = 0;
timer.Start();
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
int indx = r.Next(0, images.Count - 1);
ARandomNumber = images[index];
images.RemoveAt(indx);
string path = @"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\";
pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
indx = r.Next(0, images.Count - 1);
ARandomNumber = images[index];
images.RemoveAt(index);
pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");
indx = r.Next(0, images.Count - 1);
ARandomNumber = images[index];
images.RemoveAt(index);
pictureBox5.Image = Image.FromFile(path + ARandomNumber + ".png");
//Console.WriteLine(ARandomNumber);
if (images.Count <= 1)
{
images.Clear();
populateImag();
}
}
public void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter == 1)
//or whatever amount of time you want it to be invisible
{
pictureBox3.Visible = true;
}
if (counter == 2)
{
pictureBox4.Visible = true;
}
if (counter == 3)
{
pictureBox5.Visible = true;
timer.Stop();
counter = 0;
}
}
public void populateImag()
{
List<Image> images = new List<Image>();//add images to this array
DirectoryInfo di = new DirectoryInfo(@"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image"); // give path
FileInfo[] finfos = di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
foreach (FileInfo fi in finfos)
images.Add(Image.FromFile(fi.FullName));
}
}
}
提前感谢您的帮助。