从文件夹中选择随机图像以显示在图片框vb.net中 [英] select random image from folder to display in picturebox, vb.net

查看:213
本文介绍了从文件夹中选择随机图像以显示在图片框vb.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片框,它可以从文件夹中读取图像进行显示,而不是像平常那样令人厌烦的图像,我认为在文件夹中包含许多图像并让我的vb.net程序随机选择可能会很好一次使用.

I have a picture box which reads in an image from a folder to display, instead of having the usual boring image I thought it may be nice to have a number of images in the folder and let my vb.net program randomly pick one out to use.

我该怎么做?

推荐答案

尝试一下:

Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
    Dim files() As String = Directory.GetFiles(folderPath, "*.png")
    Dim random As Random = New Random()
    Return files(random.Next(0, files.Length - 1))
End Function

仅供参考,如果您要多次调用它,最好以类的私有成员身份创建一次random,这样它就不会在每次调用时重新设定随机数的生成.

FYI, if you are going to call it multiple times, it would be better to create random once as a private member of the class so that it doesn't reseed the random number generation every time it's called.

这篇关于从文件夹中选择随机图像以显示在图片框vb.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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