1

我想从我的电脑文件夹中随机显示一张图片,但每张图片只能显示一次

这可能吗?

这是我设法找到的:

Dim rnd As New Random
    Dim alljpgs() As String = IO.Directory.GetFiles("C:\ImageFolder", "*.*", IO.SearchOption.TopDirectoryOnly)
    Dim jpg As New Bitmap(alljpgs(rnd.Next(0, alljpgs.Count - 1)))
    PictureBox1.BackgroundImage = jpg

提前致谢!

4

1 回答 1

0
Dim rnd As New Random
Dim alljpgs() As String = IO.Directory.GetFiles("C:\ImageFolder", "*.*", IO.SearchOption.TopDirectoryOnly)
Dim seenJpgs as new List(of string)
Dim jpg As Bitmap
do
    jpg = New Bitmap(alljpgs(rnd.Next(0, alljpgs.Count - 1)))
    if not seenJpgs.any(function(seenJpg) seenJpg = jpg) then
        PictureBox1.BackgroundImage = jpg
        seenJpgs.add(jpg)
        break
    end if
loop while true

您需要在调用中保留 seenJpgs 变量。执行此操作的方法很大程度上取决于您使用的技术(asp.net、wpf、winforms 等),但如果您在有状态的应用程序中,最简单的解决方案是将其放在一个类中并将 seenJpgs 声明为共享属性。

于 2013-10-25T20:18:56.650 回答