图片框C#的随机输出图像 [英] Random out image for picture box c#

查看:85
本文介绍了图片框C#的随机输出图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个装有此图片框的Winform上工作.我有52张不同的图像,并且在此特定的图片框中将仅显示1张图像.我不确定在不以52 if语句结束的情况下应该如何做.任何人都可以帮我解决这个问题,因为我在编程方面还是个新手:)

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 :)

我正在用C#编程

谢谢! :D

推荐答案

第一步是列出某种形式的列表,以存储所有图像.您可以选择图像列表或它们的路径列表.

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.

如果使用图像路线,则可以使用List<Image> images = new List<Image>();创建图像列表,并为每个image使用images.Add(image);将每个图像添加到图像列表中.

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.

如果使用路径路由,则可以使用List<String> paths = new List<String>();创建路径列表,并使用paths.Add(path);为每个path添加每个图像.

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.

对于图像:

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所说,使用路径是一个更好的主意(从内存使用的角度来看),除非您是动态创建图像的,或者由于其他原因已经拥有了图像.

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.

这篇关于图片框C#的随机输出图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆