Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check A Picturebox For A Specific Image Name

I need to check a picturebox for a specific image. I know you can check if the picturebox is populated with an image...

If Not pictureBox.Image is Nothing Then

Else

End If

But in my case, I need to check this picturebox for an image I loaded earlier on in the process.

Here is the current code I'm using to load the image...

PictureBox1.Image = My.Resources.TestImage1

I thought by using the following code I could check the image name, but this apparently does not work.

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

Suggestions?

like image 799
Muhnamana Avatar asked Jan 30 '13 14:01

Muhnamana


2 Answers

Image does not have any knowledge of the file name or any other name that it has been loaded from. What you can do, however, is compare images pixel-by-pixel. Try this code:

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

Credit goes here.

A useful article I found when looking for this answer:

  • Compare two images to find differences greater than a threshold value in VB .NET

This is how you can check if your images are less than 100% equal, i.e. similar.

like image 58
Neolisk Avatar answered Nov 03 '22 19:11

Neolisk


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

..................Just try it! :)

like image 2
user3184359 Avatar answered Nov 03 '22 19:11

user3184359