如何进行随机图像比较

时间:2019-06-13 06:44:09

标签: vb.net

我希望同一张图片显示一个名为“ Ok”的消息框。即使出现相同的图片,MsgBox也会显示“否”。

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a, b As Object
        a = PictureBox1.Image
        b = PictureBox2.Image
        If a Is b Then
            MsgBox("Ok")
        Else
            MsgBox("No")
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PictureBox1.Image = New Bitmap("C:\vb\pic_compare\gg.png")
        PictureBox2.Image = PictureBox1.Image
        PictureBox1.Image = New Bitmap("C:\vb\pic_compare\ww.png")
        PictureBox2.Image = PictureBox1.Image
        PictureBox1.Image = New Bitmap("C:\vb\pic_compare\aa.jfif")
        PictureBox2.Image = PictureBox1.Image
        PictureBox1.Image = New Bitmap("C:\vb\pic_compare\bb.jfif")
        PictureBox2.Image = PictureBox1.Image
        PictureBox1.Image = New Bitmap("C:\vb\pic_compare\c.jfif")
        PictureBox2.Image = PictureBox1.Image
        PictureBox1.Image = New Bitmap("C:\vb\pic_compare\ss.jfif")
        PictureBox2.Image = PictureBox1.Image
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Randomize()

ReRnd:
        Dim r1 As Object = Int(Rnd(1) * 6) + 1
        Dim r2 As Object = Int(Rnd(1) * 6) + 1
        Select Case r1
            Case 1
                PictureBox1.Image = New Bitmap("C:\vb\pic_compare\gg.png")
                Exit Select
            Case 2
                PictureBox1.Image = New Bitmap("C:\vb\pic_compare\ww.png")
                Exit Select
            Case 3
                PictureBox1.Image = New Bitmap("C:\vb\pic_compare\aa.jfif")
                Exit Select
            Case 4
                PictureBox1.Image = New Bitmap("C:\vb\pic_compare\bb.jfif")
                Exit Select
            Case 5
                PictureBox1.Image = New Bitmap("C:\vb\pic_compare\c.jfif")
                Exit Select
            Case 6
                PictureBox1.Image = New Bitmap("C:\vb\pic_compare\ss.jfif")
                Exit Select
        End Select

        Select Case r2
            Case 1
                PictureBox2.Image = New Bitmap("C:\vb\pic_compare\gg.png")
                Exit Select
            Case 2
                PictureBox2.Image = New Bitmap("C:\vb\pic_compare\ww.png")
                Exit Select
            Case 3
                PictureBox2.Image = New Bitmap("C:\vb\pic_compare\aa.jfif")
                Exit Select
            Case 4
                PictureBox2.Image = New Bitmap("C:\vb\pic_compare\bb.jfif")
                Exit Select
            Case 5
                PictureBox2.Image = New Bitmap("C:\vb\pic_compare\c.jfif")
                Exit Select
            Case 6
                PictureBox2.Image = New Bitmap("C:\vb\pic_compare\ss.jfif")
                Exit Select
        End Select
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

问题是您一直在不断创建新的Bitmap对象。如果您从同一个文件中创建两个不同的Bitmap对象,那么它们是两个不同的对象,因此测试一个Is另一个显然是否为False,从而得出结果。

您应该做的就是从每个文件中创建一个Bitmap对象,并将其存储以供重用。从逻辑上讲,您应该将它们存储在数组中,然后可以将生成的随机数用作该数组的索引。如果您在两个Bitmap中使用相同的PictureBoxes对象,则另一个Is使用一个对象,您将得到想要的结果。

举一个现实的例子,说明为什么您做错了事,请考虑您和我去找汽车经销商并且乘汽车在各个方面都相同的情况。这是否意味着我的车就是你的车,反之亦然?当然不是。他们是两辆相同的汽车。这就是您对Bitmap对象所做的事情。

Imports System.IO

Public Class Form1

    Private rng As New Random
    Private images As Bitmap()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim folderPath = "C:\vb\pic_compare"

        images = {New Bitmap(Path.Combine(folderPath, "gg.png")),
                  New Bitmap(Path.Combine(folderPath, "ww.png")),
                  New Bitmap(Path.Combine(folderPath, "aa.jfif")),
                  New Bitmap(Path.Combine(folderPath, "bb.jfif")),
                  New Bitmap(Path.Combine(folderPath, "c.jfif")),
                  New Bitmap(Path.Combine(folderPath, "ss.jfif"))}
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim max = images.Length
        Dim index1 = rng.Next(max)
        Dim index2 = rng.Next(max)

        PictureBox1.Image = images(index1)
        PictureBox2.Image = images(index2)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If PictureBox1.Image Is PictureBox2.Image Then
            MessageBox.Show("OK")
        Else
            MessageBox.Show("No")
        End If
    End Sub

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        'Dispose the Images to release the files.
        For Each bmp In images
            bmp.Dispose()
        Next
    End Sub

End Class