检查特定图像名称的Picturebox

时间:2013-01-30 14:07:42

标签: vb.net embedded-resource picturebox

我需要检查图片框中的特定图片。我知道你可以检查图片框是否填充了图片......

If Not pictureBox.Image is Nothing Then

Else

End If

但在我的情况下,我需要检查此图片框以查看我之前加载的图像。

以下是我用于加载图片的当前代码...

PictureBox1.Image = My.Resources.TestImage1

我想通过使用以下代码我可以检查图像名称,但这显然不起作用。

If PictureBox1.Image = My.Resources.TestImage1 Then
  'do something
Else
  'do something else
End if

建议?

2 个答案:

答案 0 :(得分:5)

图片不知道文件名或从中加载的任何其他名称。但是,您可以做的是逐个像素地比较图像。试试这段代码:

Public Function AreSameImage(ByVal I1 As Image, ByVal I2 As Image) As Boolean
  Dim BM1 As Bitmap = I1
  Dim BM2 As Bitmap = I2
  For X = 0 To BM1.Width - 1
    For y = 0 To BM2.Height - 1
      If BM1.GetPixel(X, y) <> BM2.GetPixel(X, y) Then
        Return False
      End If
    Next
  Next
  Return True
End Function

积分here

我在寻找这个答案时找到的一篇有用的文章:

这是您可以检查图像是否小于100%相等的方式,即类似。

答案 1 :(得分:2)

Dim a as image=my.resources.image1.jpg' imported file from resources
Dim b as image=my.resources.image2.jpg' imported file from resources

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

picturebox1.image=a
picturebox2.image=b

end sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

   if picturebox1.image is a and picturebox2.image=b then

      picturebox2.image=a
      picturebox1.image=nothing
   else
      picturebox2.image=b
      picturebox1.image=a
end if
end sub

..................试试吧! :)