如何随机更改几个图片框的图片

时间:2019-07-18 06:34:24

标签: vb.net

我基本上想知道如何优化此代码。我试图以某种方式创建一个程序,该程序可以更改图片框的照片,但要使用随机数发生器。基本上,每张照片都有一定的几率出现​​在图片框中,经过几次尝试,对我唯一有用的是这个丑陋且优化欠佳的代码,但是如果我尝试对其进行修复,则该程序只会失败。好吧,我的代码生成了一个从1到100的随机数,并根据生成的数字选择了六张照片中的一张。它正在工作,但是它仅从一个图片框更改图像,并且我要切换50个以上(仍然是随机的)。我把它更改为一个按钮即可更改所有内容,这花了我一个小时的时间,而且我发现程序变得非常慢,所以这是不可能的。有什么方法可以只使用一个代码一次更改所有内容,但可以随机保存所有50个图片框?

想法是,在50个图片框的末尾随机填充照片,可以重复的图片,但是其中一些图片的出现可能性比其他图片小。

我知道,这有点令人困惑,但是即使有人可以帮助我,也要谢谢你。

我的代码:

randomvalue = random.Next(1, 101)
    If randomvalue = "1" Or randomvalue = "2" Or randomvalue = "3" Or randomvalue = "4" Or randomvalue = "5" Or randomvalue = "6" Or randomvalue = "7" Or randomvalue = "8" Or randomvalue = "9" Or randomvalue = "10" Or randomvalue = "11" Or randomvalue = "12" Or randomvalue = "13" Or randomvalue = "14" Or randomvalue = "15" Or randomvalue = "16" Or randomvalue = "17" Or randomvalue = "18" Or randomvalue = "19" Or randomvalue = "20" Or randomvalue = "21" Or randomvalue = "22" Or randomvalue = "23" Or randomvalue = "24" Or randomvalue = "25" Or randomvalue = "26" Or randomvalue = "27" Or randomvalue = "28" Or randomvalue = "29" Or randomvalue = "30" Or randomvalue = "31" Then
        btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img1.png")
    End If

    If randomvalue = "32" Or randomvalue = "33" Or randomvalue = "34" Or randomvalue = "35" Or randomvalue = "37" Or randomvalue = "38" Or randomvalue = "39" Or randomvalue = "40" Or randomvalue = "41" Or randomvalue = "42" Or randomvalue = "43" Or randomvalue = "44" Or randomvalue = "45" Or randomvalue = "46" Or randomvalue = "47" Or randomvalue = "48" Or randomvalue = "49" Or randomvalue = "50" Or randomvalue = "51" Or randomvalue = "52" Or randomvalue = "53" Or randomvalue = "54" Or randomvalue = "55" Or randomvalue = "56" Or randomvalue = "57" Then
        btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img2.png")
    End If

    If randomvalue = "58" Or randomvalue = "59" Or randomvalue = "60" Or "61" Or randomvalue = "62" Or randomvalue = "63" Or randomvalue = "64" Or randomvalue = "65" Or randomvalue = "66" Or randomvalue = "67" Or randomvalue = "68" Or randomvalue = "69" Or randomvalue = "70" Or randomvalue = "71" Or randomvalue = "72" Or randomvalue = "73" Or randomvalue = "74" Or randomvalue = "75" Or randomvalue = "76" Or randomvalue = "77" Then
        btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img3.png")
    End If

    If randomvalue = "78" Or randomvalue = "79" Or "80" Or randomvalue = "81" Or randomvalue = "82" Or randomvalue = "83" Or randomvalue = "84" Or randomvalue = "85" Or randomvalue = "86" Or randomvalue = "87" Or randomvalue = "88" Or randomvalue = "89" Or randomvalue = "90" Then
        btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img4.png")
    End If

    If randomvalue = "91" Or randomvalue = "92" Or randomvalue = "93" Or randomvalue = "94" Or randomvalue = "95" Or randomvalue = "96" Or randomvalue = "97" Or randomvalue = "98" Or randomvalue = "99" Or randomvalue = "100" Then
        btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img5.png")
    End If

    If randomvalue = "36" Then
        btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img6.png")
    End If

1 个答案:

答案 0 :(得分:0)

我不知道实际的数字是什么,因为我不会打扰计数,而是说您的六张图像应该有10%,20%,10%,20%,10%和30%的几率分别使用。您可以使用Select Case轻松过滤随机数,例如

Select Case myRandom.Next(0, 100)
    Case Is < 10 '0-9
        'Use first image
    Case Is < 30 '10-29
        'Use second image
    Case Is < 40 '30-39
        'Use third image
    Case Is < 60 '40-59
        'Use fourth image
    Case Is < 70 '60-69
        'Use fifth image
    Case Else '70-99
        'Use sixth image
End Select

还值得注意的是Random类是可继承的,因此您实际上可以创建自己的类,该类允许您指定结果的权重。实际上,我之前已经创建了WeightedRandom类,但似乎没有将其保留在任何地方。我认为我必须再次这样做,并将其放置在可公开访问的位置。

编辑:这是一个更完整的示例:

Imports System.IO

Public Class Form1

    Private folderPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "imgsx")
    Private fileNames As String() = {"img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png"}
    Private imagesByFileName As New Dictionary(Of String, Image)
    Private rng As New Random

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Create one Image object per file.
        For Each fileName In fileNames
            Dim filePath = Path.Combine(folderPath, fileName)
            Dim img = Image.FromFile(filePath)

            imagesByFileName.Add(fileName, img)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Display a random image in each PictureBox on the form.
        For Each pictureBox In Controls.OfType(Of PictureBox)
            pictureBox.Image = GetWeightedRandomImage()
        Next
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'Clean up the Images to release the files.
        For Each fileName In fileNames
            imagesByFileName(fileName).Dispose()
        Next
    End Sub

    Private Function GetWeightedRandomImage() As Image
        Dim fileNameIndex As Integer

        Select Case rng.Next(0, 100)
            Case Is < 10 '0-9
                fileNameIndex = 0
            Case Is < 30 '10-29
                fileNameIndex = 1
            Case Is < 40 '30-39
                fileNameIndex = 2
            Case Is < 60 '40-59
                fileNameIndex = 3
            Case Is < 70 '60-69
                fileNameIndex = 4
            Case Else '70-99
                fileNameIndex = 5
        End Select

        Return imagesByFileName(fileNames(fileNameIndex))
    End Function

End Class