Vb.net将PictureBox中的图片移动到另一个PictureBox

时间:2016-07-31 14:22:31

标签: vb.net

我正在vb.net制作游戏,我正试图将图片框中的图片移动到另一个图片框。但我无法让它发挥作用。

Dim ChestPlate As Image = My.Resources.ChestPlate

Private Sub pictureBox1_Click(sender As Object, e As EventArgs) Handles pictureBox1.Click

    If pictureBox1.Image Is ChestPlate Then

        pictureBox2.Image = ChestPlate

        pic1tureBox1.Image = Nothing
    End If
End Sub

1 个答案:

答案 0 :(得分:-1)

很遗憾,你只能使用" ="来比较图像。在.NET中。 StackOveflow有很多关于此的答案。您可以使用第三方库来执行您想要的操作。您也可以查看此Microsoft库链接,虽然我不知道它是否可行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

然后是您的点击事件:

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

同样为了将来的参考,&#34;是&#34;关键字不检查值相等而不是对象相等,因此即使图像是相同的,因为它们是不同的对象,它将返回false。C# IS Keyword