Random out image for picture box c#

前端 未结 2 1630
感动是毒
感动是毒 2021-01-14 23:55

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 sur

相关标签:
2条回答
  • 2021-01-15 00:33

    Little example:

    // 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();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-15 00:39

    The first step would be to make a list of some sort to store all your images. You can either opt for a list of Images, or a list of their paths.

    If you're using the Image route, you can create a list of images with List<Image> images = new List<Image>(); and add each image to it with images.Add(image); for each image.

    If you're using the path route, you can create a list of paths with List<String> paths = new List<String>(); and add each image to it with paths.Add(path); for each path.

    Then, when you're setting the picture box to a random image, you can generate a random number and pick one out of the list.

    For Images:

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

    For paths:

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

    As Tuukka says, using paths is a much better idea (memory usage-wise), unless you've created the images dynamically, or already have the images for some other reason.

    0 讨论(0)
提交回复
热议问题