2016-07-31 148 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