2016-07-31 102 views
0

我正在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 
+0

什么不行?它有bug吗?照片二只保留空白还是两张照片都空白? – mike100111

+0

什么都没发生,picturebox1有图片,而picturebox2没有图片。 – Grim

回答

-1

不幸的是,你不能通过在.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

+0

对不起,我很新vb。当我输入“如果AreSameImage(pictureBox1,ChestPlate)然后”然后“这是一个pictureBox1错误。 “类型'System.Windows.Forms.PictureBox'的值不能转换为'System.Drawing.bitmap'。 – Grim

+0

对不起,你需要picturebox1.image。我会更正我的答案 – mike100111

+0

非常感谢! – Grim