0

我正在使用此代码在 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));
        }
    }
}

提前感谢您的帮助。

4

3 回答 3

2

ARandomNumber 是一个图像,它不是一个路径。图像是加载到内存中的资源,并不总是链接到路径。

这是行不通的,因为 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 图像吗?

编辑:可能的解决方案是将文件路径作为字符串加载,而不是加载图像或者(因为你有一个数组中的所有图像),试试这个:

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.
于 2013-07-29T12:21:36.897 回答
0

在选择图像文件时,您正在选择“.jpg”图像,而在加载时,您正在将“.png”扩展名附加到这些文件中。我认为这会造成麻烦。查看错误:

pictureBox3.image C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\System.Drawing.Bitmap.png

具有 2 个扩展名的文件名。.Bitmap.png

于 2013-07-29T12:23:01.653 回答
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而不是DirectoryInfoand FileInfo

private List<string> GetFilesToShow()
{
    string path = @"C:\some\folder";
    return Directory.GetFiles(path, "*.jpg", SearchOption.TopDirectoryOnly)
                    .ToList();
}

您需要做的所有事情 -ShowRandomImages从计时器滴答事件处理程序中调用方法。

于 2013-07-29T12:43:00.230 回答