不幸的是,你不能通过在.NET中使用“=”来比较图像。 StackOveflow上有很多关于此的答案。有第三方库可以用来做你想做的事情。你也可以看看这个微软的图书馆链接,虽然我不知道它是否会工作ImageComparer。如果您正在处理小图像,则可以比较每个像素以查看图像是否匹配。
'checks if two images are the same by comparing each pixel. not very fast for large images.
Private Function AreSameImage(ByVal bitmap1 As Bitmap, ByVal bitmap2 As Bitmap) As Boolean
For X = 0 To bitmap1.Width - 1
For y = 0 To bitmap2.Height - 1
If bitmap1.GetPixel(X, y) <> bitmap2.GetPixel(X, y) Then
Return False
End If
Next
Next
'If every pixel matched, return true
Return True
End Function
然后为click事件:
Dim ChestPlate As Image = My.Resources.ChestPlate
Private Sub pictureBox1_Click(sender As Object, e As EventArgs) Handles pictureBox1.Click
If AreSameImage(picturebox1.image ,Chestplate) Then
pictureBox2.Image = ChestPlate
pic1tureBox1.Image = Nothing
End If
End Sub
退房此链接:How to make comparison in VB.NET
而且以供将来参考“是”关键字不检查值相等,而对于对象相等,即使图像是相同的,因为它们是不同的对象,它将返回false。 C# IS Keyword
什么不行?它有bug吗?照片二只保留空白还是两张照片都空白? – mike100111
什么都没发生,picturebox1有图片,而picturebox2没有图片。 – Grim