2

I'm working on a Winform where I have this picture box. I have 52 different images and only 1 image is going to be shown in this particular picture box. I'm not really sure how I should do this without ending up with 52 if statements. Could anyone help me with this as I'm still kinda new in programming :)

I'm programming in c#

Thank you! :D

4

2 回答 2

2

小例子:

// Controls:
// pictureBox1
// Dock: Fill
// SizeMode: StretchImage
// timer1

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;

namespace RandomImg
{
    public partial class Form1 : Form
    {
        // List of files to show 
        private List<string> Files;

        public Form1()
        {
            InitializeComponent();
        }

        // StartUp 
        private void Form1_Load(object sender, EventArgs args)
        {
            // basic settings.
            var ext = new List<string> {".jpg", ".gif", ".png"};

            // we use same directory where program is.
            string targetDirectory = Directory.GetCurrentDirectory();

            // Here we create our list of files
            // New list
            // Use GetFiles to getfilenames
            // Filter unwanted stuff away (like our program)
            Files = new List<string>
                (Directory.GetFiles(targetDirectory, "*.*", SearchOption.TopDirectoryOnly)
                .Where(s => ext.Any(e => s.EndsWith(e))));

            // Create timer to call timer1_Tick every 3 seconds.
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 3000; // 3 seconds
            timer1.Start();

            // Show first picture so we dont need wait 3 secs.
            ChangePicture();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Time to get new one.
            ChangePicture();
        }

        private void ChangePicture()
        {
            // Do we have pictures in list?
            if (Files.Count > 0)
            {
                // OK lets grab first one
                string File = Files.First();
                // Load it
                pictureBox1.Load(File);
                // Remove file from list
                Files.RemoveAt(0);
            }
            else
            {
                // Out of pictures, stopping timer
                // and wait god todo someting.
                timer1.Stop();
            }
        }
    }
}
于 2013-05-11T19:27:37.900 回答
1

第一步是制作某种列表来存储所有图像。您可以选择图像列表或路径列表。

如果您使用 Image 路由,您可以创建一个图像列表,并使用for eachList<Image> images = new List<Image>();将每个图像添加到其中。images.Add(image);image

如果您正在使用路径路由,您可以创建一个路径列表,并使用for eachList<String> paths = new List<String>();将每个图像添加到其中。paths.Add(path);path

然后,当您将图片框设置为随机图像时,您可以生成一个随机数并从列表中选择一个。

对于图像:

Random random = new Random();
pictureBox1.Image = images[random.Next(0, images.Count - 1)];

对于路径:

Random random = new Random();
pictureBox1.ImageLocation = paths[random.Next(0, images.Count - 1)];

正如 Tuukka 所说,使用路径是一个更好的主意(内存使用方面),除非您已经动态创建了图像,或者由于其他原因已经有了图像。

于 2013-05-11T18:18:12.893 回答