我正在使用此代码在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));
}
}
}
提前感谢您的帮助。
答案 0 :(得分:2)
ARandomNumber是一个图像,它不是Path。图像是加载到内存中的资源,并不总是链接到路径。
这不起作用,因为ARandomNumber.ToString()没有返回路径 (我建议使用包含文件路径而不是ARandomNumber的字符串):
pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
其次,首先加载所有* .jpgs:
FileInfo[] finfos = di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
然后,加载扩展名为* .png的图像:
pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
您确定文件夹中有同名的jpg和png图片吗?
编辑: 可能的解决方案是将文件路径加载为字符串而不是加载图像OR(因为您将所有图像都放在一个数组中),试试这个:
ARandomNumber = images[index]; //Here you take a random image from the array of images
images.RemoveAt(indx);
pictureBox3.Image = ARandomNumber; //Here you assign the image directly to the Image property in picturebox. You do not need to load it again from file.
答案 1 :(得分:0)
选择图像文件时,您正在选择“.jpg”图像,在加载时,您将“.png”扩展名附加到这些文件。我认为这会造成麻烦。 看看错误:
pictureBox3.image C:\ Users \ Monika \ Documents \ Visual Studio 2010 \ Projects \ OnlineExam \ OnlineExam \ Image \ System.Drawing.Bitmap.png
具有2个扩展名的文件名。 .Bitmap.png
答案 2 :(得分:0)
在设计器中添加计时器,设置它的间隔并在设计器中订阅它的Tick事件 - 这将使您的代码更清晰。下一步 - 创建要用于显示随机图像的图片框列表:
public partial class Form1 : Form
{
Random random = new Random();
List<string> filesToShow = new List<string>();
List<PictureBox> pictureBoxes;
public Form1()
{
InitializeComponent();
pictureBoxes = new List<PictureBox> {
pictureBox1,
pictureBox2,
pictureBox3
};
ShowRandomImages();
timer1.Start();
}
}
从构造函数调用的唯一方法是ShowRandomImages
,其定义为:
private void ShowRandomImages()
{
foreach (var pictureBox in pictureBoxes)
{
if (!filesToShow.Any())
filesToShow = GetFilesToShow();
int index = random.Next(0, filesToShow.Count);
string fileToShow = filesToShow[index];
pictureBox.ImageLocation = fileToShow;
filesToShow.RemoveAt(index);
}
}
此方法从位置分配随机图像。如果没有要显示的文件,则重新加载列表。请注意 - 如果您只需要文件名,请使用Directory
代替DirectoryInfo
和FileInfo
:
private List<string> GetFilesToShow()
{
string path = @"C:\some\folder";
return Directory.GetFiles(path, "*.jpg", SearchOption.TopDirectoryOnly)
.ToList();
}
您需要做的一切 - 从计时器刻度事件处理程序调用{{1}}方法。