Vb.net 如何进行随机图像比较
我希望相同的图片显示一个名为“Ok”的msgbox。即使出现相同的图片,MsgBox也会显示“否”Vb.net 如何进行随机图像比较,vb.net,Vb.net,我希望相同的图片显示一个名为“Ok”的msgbox。即使出现相同的图片,MsgBox也会显示“否” 问题是您一直在创建新的位图对象。如果从同一个文件创建两个不同的位图对象,则它们是两个不同的对象,因此测试一个是否为,另一个显然是为假,因此您会看到结果 您应该做的只是从每个文件创建一个位图对象,并存储这些对象以供重用。从逻辑上讲,您应该将它们存储在一个数组中,然后可以使用生成的随机数作为该数组的索引。如果在两个PictureBoxes中使用相同的Bitmap对象,则其中一个就是另一个,您将得到想要
问题是您一直在创建新的
位图对象。如果从同一个文件创建两个不同的位图
对象,则它们是两个不同的对象,因此测试一个是否为
,另一个显然是为假
,因此您会看到结果
您应该做的只是从每个文件创建一个位图
对象,并存储这些对象以供重用。从逻辑上讲,您应该将它们存储在一个数组中,然后可以使用生成的随机数作为该数组的索引。如果在两个PictureBoxes
中使用相同的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
问题是您一直在创建新的位图对象。如果从同一个文件创建两个不同的位图
对象,则它们是两个不同的对象,因此测试一个是否为
,另一个显然是为假
,因此您会看到结果
您应该做的只是从每个文件创建一个位图
对象,并存储这些对象以供重用。从逻辑上讲,您应该将它们存储在一个数组中,然后可以使用生成的随机数作为该数组的索引。如果在两个PictureBoxes
中使用相同的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
您可以安全地删除代码中的每个Exit Select
。VB.Net不会通过选择
&只会执行匹配的案例
。同时随机化可能会为两者提供相同的值,请尝试将其设置为静态,而不是仅变暗。顺便说一句,你在formLoad上所做的事情不会导致点击时发生的事情,如果是为了视觉效果,你可以保留最后两行。正如Pete所说,exit select在此不起任何作用,您可以安全地删除代码中的每个exit select
。VB.Net不会通过选择
&只会执行匹配的案例
。同时随机化可能会为两者提供相同的值,请尝试将其设置为静态,而不是仅变暗。顺便说一句,你在formLoad上所做的事情不会导致点击时发生的事情,如果是为了视觉效果,你可以保留最后两行。正如Pete所说,exit select在这里什么都不做,我添加了代码。除此之外,它以“正确”的方式生成随机数,即通过random
类。我添加了代码。除其他外,它以“正确”的方式生成随机数,即通过random
类。