且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将随机图像加载并显示到c#中的图片框中?

更新时间:2022-11-08 22:04:22

如果你使用图像路线,您可以使用列表 images = new List ()创建图像列表;并使用images.Add(image)添加每个图像。对于每个图像。



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

For Images:

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


试试这个:

Try this:
private Random rand = new Random();
...
public void GetRandomImage()
    {
    string[] files = Directory.GetFiles(@"D:\Temp\", "*.jpg", SearchOption.AllDirectories);
    myPictureBox.Image = Image.FromFile(files[rand.Next(files.Length)]);
    }


  1. 创建图像名称列表(您可以使用 DirectoryInfo.EnumerateFiles方法 [ ^ ]以实现它。)
  2. 0 (List.Count-1)之间选择一个随机数 r
  3. 加载并显示与列表项 r 相对应的图像。
  4. 等待按钮单击
  5. 转到第2点。
  1. Create a list of image names (you may use the DirectoryInfo.EnumerateFiles method[^] in order to accomplish it).
  2. Pick a random number r, between 0 and (List.Count-1).
  3. Load and show the image corrensponding to the item r of the list.
  4. Wait for button click
  5. Go to point 2.