none
how to make comparison between pictures in VB.NET?

    Question

  • Hello everybody...

    What code should I use to compare two pictures?

    I tried those:

    If PictureBox1.Image <> PictureBox2.Image then
    
    End If
    
    'and I tried this too:
    
    If PictureBox1.image IsNot PictureBox2.Image then
    
    End If
    
    'But that all did nothing
    


    ...If you did nothing,You equal nothing...
    Saturday, October 16, 2010 12:55 PM

Answers

  • It depend what you want to know,

    John code will return "different image" for 2 image that are identical but are not the same object

    This returns "Different image" even if they are identical ...  since they are not the same object

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        PictureBox2.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        CompareImages(PictureBox1.Image, PictureBox2.Image)
      End Sub
      Sub CompareImages(ByVal Img1 As Image, ByVal Img2 As Image)
        If Img1 Is Img2 Then
          MessageBox.Show("Same image")
        Else
          MessageBox.Show("Different images")
        End If
      End Sub
    End Class
    

     

    this code will return true for 2 image that are not the same object but are identical

     

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        PictureBox2.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        Dim a As Boolean = AreSameImage(PictureBox1.Image, PictureBox2.Image)
        If a Then
          MsgBox("Identical image")
        Else
          MsgBox("Different images")
        End If
      End Sub
    
      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
    End Class
    
    • Marked as answer by Eye Code Saturday, October 16, 2010 2:07 PM
    Saturday, October 16, 2010 2:01 PM
  • This works for me:

    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = 
    New Bitmap(100, 100)
        PictureBox2.Image = 
    New Bitmap(100, 100)
        CompareImages(PictureBox1.Image, PictureBox2.Image)
        PictureBox2.Image = PictureBox1.Image
        CompareImages(PictureBox1.Image, PictureBox2.Image)
      
    End Sub
      Sub CompareImages(ByVal Img1 As Image, ByVal Img2 As Image)
        
    If Img1 Is Img2 Then
          MessageBox.Show("Same image")
        
    Else
          MessageBox.Show("Different images")
        
    End If
      End Sub
    End
     Class

    • Marked as answer by Eye Code Saturday, October 16, 2010 1:53 PM
    Saturday, October 16, 2010 1:43 PM

All replies

  • This works for me:

    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = 
    New Bitmap(100, 100)
        PictureBox2.Image = 
    New Bitmap(100, 100)
        CompareImages(PictureBox1.Image, PictureBox2.Image)
        PictureBox2.Image = PictureBox1.Image
        CompareImages(PictureBox1.Image, PictureBox2.Image)
      
    End Sub
      Sub CompareImages(ByVal Img1 As Image, ByVal Img2 As Image)
        
    If Img1 Is Img2 Then
          MessageBox.Show("Same image")
        
    Else
          MessageBox.Show("Different images")
        
    End If
      End Sub
    End
     Class

    • Marked as answer by Eye Code Saturday, October 16, 2010 1:53 PM
    Saturday, October 16, 2010 1:43 PM
  • It depend what you want to know,

    John code will return "different image" for 2 image that are identical but are not the same object

    This returns "Different image" even if they are identical ...  since they are not the same object

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        PictureBox2.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        CompareImages(PictureBox1.Image, PictureBox2.Image)
      End Sub
      Sub CompareImages(ByVal Img1 As Image, ByVal Img2 As Image)
        If Img1 Is Img2 Then
          MessageBox.Show("Same image")
        Else
          MessageBox.Show("Different images")
        End If
      End Sub
    End Class
    

     

    this code will return true for 2 image that are not the same object but are identical

     

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        PictureBox2.Image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")
        Dim a As Boolean = AreSameImage(PictureBox1.Image, PictureBox2.Image)
        If a Then
          MsgBox("Identical image")
        Else
          MsgBox("Different images")
        End If
      End Sub
    
      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
    End Class
    
    • Marked as answer by Eye Code Saturday, October 16, 2010 2:07 PM
    Saturday, October 16, 2010 2:01 PM
  • Crazypennie has the correct answer as to why you were not getting the expected results.  I would just like to add an alternate routine which gives better performance when actually comparing the two images for visual equality.

    The idea is the same; compare the pixels to see if they are all the same color at the same position.  But GetPixel() and SetPixel() are slow.  All we really need to do are get the two arrays of bytes that represent the color information in the image and then see if there are any differences between the two arrays.  We can use a System.Drawing.Imaging.BitmapData object and the System.Runtime.Interop.Marshal.Copy() method to make this fairly simple.

    Note that the following routine makes some minor assumptions about the image.  Look up the "Stride" or "Scan Width" of an image for more details.  Although there could be some exceptions, this routine should work well with most images:

      Public Function IsVisuallyEqual(ByVal sourceImage As Image, ByVal sourceBounds As Rectangle, _
                          ByVal targetImage As Image, ByVal targetBounds As Rectangle) As Boolean
        Dim sourceBmp = New Bitmap(sourceImage)
        Dim targetBmp = New Bitmap(targetImage)
    
        Dim sourceData As System.Drawing.Imaging.BitmapData = sourceBmp.LockBits( _
          sourceBounds, Imaging.ImageLockMode.ReadWrite, sourceImage.PixelFormat)
        Dim targetData As System.Drawing.Imaging.BitmapData = targetBmp.LockBits( _
          targetBounds, Imaging.ImageLockMode.ReadWrite, targetImage.PixelFormat)
    
        Dim sourceLength As Integer = (sourceData.Height * sourceData.Stride) - 1
        Dim sourceBytes(sourceLength) As Byte
        System.Runtime.InteropServices.Marshal.Copy(sourceData.Scan0, sourceBytes, 0, sourceBytes.Length)
        sourceBmp.UnlockBits(sourceData)
        sourceBmp.Dispose()
    
        Dim targetLength As Integer = (targetData.Height * targetData.Stride) - 1
        Dim targetBytes(targetLength) As Byte
        System.Runtime.InteropServices.Marshal.Copy(targetData.Scan0, targetBytes, 0, targetBytes.Length)
        targetBmp.UnlockBits(targetData)
        targetBmp.Dispose()
    
        For i As Integer = 0 To sourceBytes.Length - 1
          If Not targetBytes(i) = sourceBytes(i) Then
            Return False
          End If
        Next
    
        Return True
      End Function

    Also note that since the routine expects the bounds of each image (normally a Rectangle(0, 0, Image.Width, Image.Height) in size) you could in theory pass a rectangle within an image to see if one image is cropped out of another.  You would need to know the starting point of the cropped image, or you could write an algorithm to search for it, but this routine would allow you to match the partial image given its known starting point.

    Anyway, like I said, pennie already did a good job answering the thread.  Since this topic will likely get lots of search hits over time, I thought this extra bit of code might be a good addition.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Sunday, October 17, 2010 6:57 AM
    Moderator
  •  

    Eye Code,

    Reed is just being polite with me, His answer is the right way to do that.

     Use the code he gave you.

     

    Sunday, October 17, 2010 11:06 AM
  • Crazypennie,

    I'm not so sure of that, your sample does not use reflection (late binding).

    I found your way a very inventive way, my idea was in fact the same but much more difficult than your approach.

    Nothing about the sample from Reed, but I found your sample very good and simple and it does what it needs to do, comparing the pixels in a bitmap).

    I assume you have tested it?

    :-)


    Success
    Cor
    Sunday, October 17, 2010 12:31 PM
  • Cor

    Always test my code before posting.

    I certainnely would have use a code similar to Reed one to test the similarity of two images in one of my application

    I agree that it is more complicated but it is much faster

    The method I showed would just be suitable to compare small images

     

    Sunday, October 17, 2010 1:10 PM
  • There are many images that can be visually identical without all of the pixels being the same.  This code will do the same comparison as Crazypennie's and Reed Kimble's and is quite quick:

      Public Function AreSameImage(ByVal I1 As Image, ByVal I2 As Image) As Boolean
        Dim MS1 As New MemoryStream
        
    Dim MS2 As New MemoryStream
        I1.Save(MS1, ImageFormat.Bmp)
        I2.Save(MS2, ImageFormat.Bmp)
        
    For I As Integer = 0 To CInt(MS1.Length) - 1
          
    If MS1.ReadByte() <> MS2.ReadByte Then Return False
        Next
        Return True
      End Function

    Sunday, October 17, 2010 2:08 PM
  •  

    Good Idea John :)

    Sunday, October 17, 2010 2:30 PM
  • Crazypennie,

    I took both methods, yours (a little bit changed in the way I would do it) and the one from Reed to compare the speed.

    However the one from Reed gave an equal with unequal images

    Maybe do you see why 

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim byteArray1() As Byte
        Dim byteArray2() As Byte
        Using fo As New OpenFileDialog
          fo.Filter = "JPG (*.jpg)|*.jpg|Gif (*.gif)|*.gif|All files (*.*)|*.*"
          If fo.ShowDialog = DialogResult.OK Then
    
            Using fs As New IO.FileStream(fo.FileName, IO.FileMode.Open)
              Using br As New IO.BinaryReader(fs)
                byteArray1 = br.ReadBytes(CInt(fs.Length))
              End Using
            End Using
            'just to show the sample without a fileread error
            Dim ms As New IO.MemoryStream(byteArray1)
            Me.PictureBox1.Image = Image.FromStream(ms)
          End If
        End Using
    
        Using fo As New OpenFileDialog
          fo.Filter = "JPG (*.jpg)|*.jpg|Gif (*.gif)|*.gif|All files (*.*)|*.*"
          If fo.ShowDialog = DialogResult.OK Then
    
            Using fs As New IO.FileStream(fo.FileName, IO.FileMode.Open)
              Using br As New IO.BinaryReader(fs)
                byteArray2 = br.ReadBytes(CInt(fs.Length))
              End Using
            End Using
            'just to show the sample without a fileread error
            Dim ms As New IO.MemoryStream(byteArray2)
            Me.PictureBox2.Image = Image.FromStream(ms)
          End If
        End Using
        If AreSameImage(byteArray1, byteArray2) Then
          MessageBox.Show("Equal")
        Else
          MessageBox.Show("False")
        End If
        Dim Rect1 As New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
        Dim Rect2 As New Rectangle(0, 0, PictureBox2.Image.Width, PictureBox2.Image.Height)
        If IsVisuallyEqual(PictureBox1.Image, Rect1, PictureBox2.Image, Rect2) Then
          MessageBox.Show("Equal")
        Else
          MessageBox.Show("False")
        End If
    
      End Sub
      Public Function AreSameImage(ByVal BM1 As Byte(), ByVal BM2 As Byte()) As Boolean
        If BM1.Count <> BM2.Count Then Return False
        For i = 0 To BM1.Count - 1
          If BM1(i) <> BM2(i) Then
            Return False
          End If
        Next
        Return True
      End Function
    
      Public Function IsVisuallyEqual(ByVal sourceImage As Image, ByVal sourceBounds As Rectangle, _
          ByVal targetImage As Image, ByVal targetBounds As Rectangle) As Boolean
        Dim sourceBmp = New Bitmap(sourceImage)
        Dim targetBmp = New Bitmap(targetImage)
    
        Dim sourceData As System.Drawing.Imaging.BitmapData = sourceBmp.LockBits(sourceBounds, _
          Imaging.ImageLockMode.ReadWrite, sourceImage.PixelFormat)
        Dim targetData As System.Drawing.Imaging.BitmapData = targetBmp.LockBits(targetBounds, _
          Imaging.ImageLockMode.ReadWrite, targetImage.PixelFormat)
    
        Dim sourceLength As Integer = (sourceData.Height * sourceData.Stride) - 1
        Dim sourceBytes(sourceLength) As Byte
        System.Runtime.InteropServices.Marshal.Copy(sourceData.Scan0, sourceBytes, 0, sourceBytes.Length)
        sourceBmp.UnlockBits(sourceData)
        sourceBmp.Dispose()
    
        Dim targetLength As Integer = (targetData.Height * targetData.Stride) - 1
        Dim targetBytes(targetLength) As Byte
        System.Runtime.InteropServices.Marshal.Copy(targetData.Scan0, targetBytes, 0, targetBytes.Length)
        targetBmp.UnlockBits(targetData)
        targetBmp.Dispose()
    
        If sourceBytes.Except(targetBytes).Count = 0 Then Return True
    
        Return False
      End Function
    
    End Class


    :-)
    Cor
    • Edited by Cor LigthertMVP Sunday, October 17, 2010 3:07 PM image1 first twice used
    Sunday, October 17, 2010 3:02 PM
  • That was where I was to long busy with John,

    :-)


    Success
    Cor
    Sunday, October 17, 2010 3:09 PM
  • Not entirely pennie...  Your code is perfectly valid for small images, or when the application knows that more tests will be false than true.

    There was an error in the example I posted; I took too big a shortcut and so the routine will return true when, for example, the target is a horizontally flipped copy of the source.  I found it because Cor made me look at the code again by insinuating an error - would have been easier and better to just point it out though rather than beat around the bush.  Anyway, the example has been fixed in the previous post. 

    I avoided John's method because I was concerned about meta data... since that method compares the contents of the entire file, you could potentially be comparing more than color information.  But in some testing, I could not get the routine to fail to due meta data changes so it may be a non-issue.  As for images being the same without every pixel being the same... well, that just doesn't make sense.  The human eye may not distinguish the difference in a high-res image, but if the color at a given pixel location is different in two images, than the images are different, even if imperceptibly so.

    At the end of the day, the specific code used comes down to the application's needs and your choice of two out of three of cost, speed, and quality.  On large images that will typically return "True" on a comparison, the routine I posted is fastest.  On images that will typically return "False", or on tiny images, John's code or Pennie's code will be the fastest.  If the images will almost always be different at pixel (0,0) then Pennie's routine is the fastest no matter what the image size is.

    This is why I posted my code as an "alternate" method to use and did not say that there was anything wrong with Pennie's example, other than a potential speed issue.  The point here was not to start a debate about "what's best", but rather to offer options that allow the reader to choose "what's most appropriate" for their situation.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Sunday, October 17, 2010 4:50 PM
    Moderator
  •   As for images being the same without every pixel being the same... well, that just doesn't make sense. 

    Images can be visually identical even if every pixel is different.  JPEG is a very popular compression method for images which depends upon this observation.  None of the comparison methods in this thread are very fast and comparing images on the basis of the individual pixels has little practical value.
    Sunday, October 17, 2010 5:14 PM
  •  I found it because Cor made me look at the code again by insinuating an error - would have been easier and better to just point it out though rather than beat around the bush.  "

    I don't know what you mean with insunuating an error, there was an error but I saw direct that that one was in my code.

    I've changed that in 5 minutes and saw that the results were wrong.

    I've not taken one minute to look at your code more than needed for the test, my intention was to put a stopwatch on it and that was impossible because of the wrong results. Why would I check your code for errors and change it, than the stopwatch had no sense.

    I've now taken your corrected code. The speed differences are not noticable so using a stopwatch has no sense, I took a 5 MB image for equal and a 5Mb and a different image for false.

    By the way, I took in fact a method which is more like the one from John, the only thing which is equal to CrazyPennie was that I use a for index loop.

    Public Class Form1
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim byteArray1() As Byte
      Dim byteArray2() As Byte
      Using fo As New OpenFileDialog
       fo.Filter = "JPG (*.jpg)|*.jpg|Gif (*.gif)|*.gif|All files (*.*)|*.*"
       If fo.ShowDialog = DialogResult.OK Then
    
        Using fs As New IO.FileStream(fo.FileName, IO.FileMode.Open)
         Using br As New IO.BinaryReader(fs)
          byteArray1 = br.ReadBytes(CInt(fs.Length))
         End Using
        End Using
        'just to show the sample without a fileread error
        Dim ms As New IO.MemoryStream(byteArray1)
        Me.PictureBox1.Image = Image.FromStream(ms)
       End If
      End Using
    
      Using fo As New OpenFileDialog
       fo.Filter = "JPG (*.jpg)|*.jpg|Gif (*.gif)|*.gif|All files (*.*)|*.*"
       If fo.ShowDialog = DialogResult.OK Then
    
        Using fs As New IO.FileStream(fo.FileName, IO.FileMode.Open)
         Using br As New IO.BinaryReader(fs)
          byteArray2 = br.ReadBytes(CInt(fs.Length))
         End Using
        End Using
        'just to show the sample without a fileread error
        Dim ms As New IO.MemoryStream(byteArray2)
        Me.PictureBox2.Image = Image.FromStream(ms)
       End If
      End Using
      If AreSameImage(byteArray1, byteArray2) Then
       MessageBox.Show("Equal")
      Else
       MessageBox.Show("False")
      End If
      Dim Rect1 As New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
      Dim Rect2 As New Rectangle(0, 0, PictureBox2.Image.Width, PictureBox2.Image.Height)
      If IsVisuallyEqual(PictureBox1.Image, Rect1, PictureBox2.Image, Rect2) Then
       MessageBox.Show("Equal")
      Else
       MessageBox.Show("False")
      End If
    
     End Sub
     Public Function AreSameImage(ByVal BM1 As Byte(), ByVal BM2 As Byte()) As Boolean
      If BM1.Count <> BM2.Count Then Return False
      For i = 0 To BM1.Count - 1
       If BM1(i) <> BM2(i) Then
        Return False
       End If
      Next
      Return True
     End Function
    
     Public Function IsVisuallyEqual(ByVal sourceImage As Image, ByVal sourceBounds As Rectangle, _
          ByVal targetImage As Image, ByVal targetBounds As Rectangle) As Boolean
      Dim sourceBmp = New Bitmap(sourceImage)
      Dim targetBmp = New Bitmap(targetImage)
    
      Dim sourceData As System.Drawing.Imaging.BitmapData = sourceBmp.LockBits( _
       sourceBounds, Imaging.ImageLockMode.ReadWrite, sourceImage.PixelFormat)
      Dim targetData As System.Drawing.Imaging.BitmapData = targetBmp.LockBits( _
       targetBounds, Imaging.ImageLockMode.ReadWrite, targetImage.PixelFormat)
    
      Dim sourceLength As Integer = (sourceData.Height * sourceData.Stride) - 1
      Dim sourceBytes(sourceLength) As Byte
      System.Runtime.InteropServices.Marshal.Copy(sourceData.Scan0, sourceBytes, 0, sourceBytes.Length)
      sourceBmp.UnlockBits(sourceData)
      sourceBmp.Dispose()
    
      Dim targetLength As Integer = (targetData.Height * targetData.Stride) - 1
      Dim targetBytes(targetLength) As Byte
      System.Runtime.InteropServices.Marshal.Copy(targetData.Scan0, targetBytes, 0, targetBytes.Length)
      targetBmp.UnlockBits(targetData)
      targetBmp.Dispose()
    
      For i As Integer = 0 To sourceBytes.Length - 1
       If Not targetBytes(i) = sourceBytes(i) Then
        Return False
       End If
      Next
    
      Return True
     End Function
    
    
    End Class

    Success
    Cor
    • Edited by Cor LigthertMVP Monday, October 18, 2010 6:10 PM typos in language used
    Sunday, October 17, 2010 6:47 PM
  •   As for images being the same without every pixel being the same... well, that just doesn't make sense. 

    Images can be visually identical even if every pixel is different.  JPEG is a very popular compression method for images which depends upon this observation.  None of the comparison methods in this thread are very fast and comparing images on the basis of the individual pixels has little practical value.

    The following sentence after the one you quoted:

    "The human eye may not distinguish the difference in a high-res image, but if the color at a given pixel location is different in two images, than the images are different, even if imperceptibly so."

    So we are back to Pennie's first statement:  "It depend what you want to know"

    Are the images more-or-less the same to the human eye when viewed at 100% size, or are they exactly the same pixel-for-pixel when compared at say 1000% zoom?  These are different problems, with different answers.  Compensating for JPEG compression differences is an entirely new issue, so in the context of this thread a pixel-by-pixel comparison seems most appropriate.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Sunday, October 17, 2010 6:53 PM
    Moderator
  •  so in the context of this thread a pixel-by-pixel comparison seems most appropriate.
    Nah.  In the context of this thread as per the OP's original post, an object reference comparison seems most appropriate.  The only reason I can think of for comparing two images pixel by pixel for absolute equality it to find indentical copies of the same file for disk cleaning.  This can be done more simply without opening the files as images.  For an example of the modifications that can be made to an image without noticable visible difference, see the Steganographer example on www.johnweinhold.com
    Sunday, October 17, 2010 7:40 PM
  •  so in the context of this thread a pixel-by-pixel comparison seems most appropriate.
    Nah.  In the context of this thread as per the OP's original post, an object reference comparison seems most appropriate.  The only reason I can think of for comparing two images pixel by pixel for absolute equality it to find indentical copies of the same file for disk cleaning.  This can be done more simply without opening the files as images.  For an example of the modifications that can be made to an image without noticable visible difference, see the Steganographer example on www.johnweinhold.com


    I wasn't originally trying to speculate the reason for the question...

    The OP said that they tried a reference comparison and that it did not work for them.  It would appear that they marked your post as answer because it is correct for comparing reference types, but since that is not what they needed, they also marked Pennie's post as answer because, for whatever reason, they wanted a pixel-by-pixel comparison.

    Thanks for the link though - that could add further value to another reader of the thread.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Sunday, October 17, 2010 8:08 PM
    Moderator
  • I've not taken one minute to look at your code more than needed for the test...

     

    Cor,

    My apologies.  I didn't understand why you reposted the code with the error in place if you knew it was there; I now realize that you had not found the source of the error, but had only observed its results.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Sunday, October 17, 2010 8:11 PM
    Moderator
  • Let me applaude all of you that replied to this thread!

    Very good information! :)

    Sunday, October 17, 2010 9:34 PM
  • Thank you all ^_^...

     

    I tried too much till now ^_^

     

    Thanks alot...


    ...If you did nothing,You equal nothing...
    Monday, October 18, 2010 5:20 PM
  • If you want to compare two images and see if 1 image is inside the other image and give you a percent rate of success you could use what I made.  It also moves mouse over top of the first pixel in the recognized image(larger image version)    Although this is with GetPixel and can take awhile to find the image inside a larger image (for large images) but lockbits can do the same process extremely quick.

    Try
    
                Dim ifnd As Boolean = False
                Dim PreviousX As Integer
                Dim PreviousY As Integer
                PreviousX = MousePosition.X
                PreviousY = MousePosition.Y
                Dim MatchCount As Integer = 0
    
                Dim img As Bitmap
                Dim bmg As Bitmap
    
                Dim oX As Integer = 0
                Dim oi As Integer = 0
                Dim iX As Integer = 0
                Dim iY As Integer = 0
                'large image
                img = BBt
                'small image
                bmg = Abt
    
                Thread.Sleep(1000)
                'large image
                If BBt Is Nothing Then
                    Dim xbit As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
    
                    Dim g As Graphics = Graphics.FromImage(xbit)
                    BBt = xbit
                    g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size)
                    g.Dispose()
                    Thread.Sleep(2000)
                End If
    
                'small image
                PictureBox5.BackgroundImage = Abt
                'large image
                PictureBox6.BackgroundImage = BBt
                'For value As Integer = 0 To 5
                'For value As Integer = 10 To 0 Step -2
                Thread.Sleep(1000)
                Dim MathScore As Integer
                Dim MaxScore As Integer = bmg.Height
                For outerX As Integer = 0 To img.Width - bmg.Width 'outerx 
                    For outerY As Integer = 0 To img.Height - bmg.Height ' outery
                        For innerX As Integer = 0 To bmg.Width ' innerx
                            For innerY As Integer = 0 To bmg.Height
                                ' Create a Bitmap object from an image file. 
    
    
                                ' Get the color of a pixel within myBitmap. 
                                Dim Clor As Color = bmg.GetPixel(innerX, innerY)
                                Dim C2or As Color = img.GetPixel(innerX + outerX, innerY + outerY)
    
                                If Clor.R <> C2or.R AndAlso Clor.G <> C2or.G AndAlso Clor.B <> C2or.B Then
                                    MathScore = 0
                                    GoTo NotFound
                                End If
                                If Clor.R = C2or.R AndAlso Clor.G = C2or.G AndAlso Clor.B = C2or.B Then
    
                                    MathScore = MathScore + 1
                                    If MathScore >= Val(MaxScore / 1.5) Then
                                        ifnd = True
                                        SetCursorPos(Val(outerX + 2), Val(outerY + 3))
                                        Dim percent As String
                                        Dim myDec As Decimal
                                        'inttemp = (intData2 * 100) / intData1
                                        myDec = Val((MathScore * 100) / MaxScore)
                                        myDec = FormatNumber(myDec, 0)
                                        percent = myDec & "%"
                                        Label16.Text = "Match Score: " & percent
                                        Label17.Text = "Math Score: " & MathScore & " out of " & MaxScore
                                        Me.ToolStripStatusLabel2.Text = "Completed"
                                        Me.Button4.Enabled = True
                                        GoTo Foundit
    
                                    End If
                                End If
                                Label11.Text = " OuterX: " & outerX & " OuterY: " & outerY & " InnerX: " & innerX & " InnerY: " & innerY
    
                            Next
                            Label12.Text = " OuterX: " & outerX & " OuterY: " & outerY & " InnerX: " & innerX
                            MathScore = 0
                        Next
    
                        Label13.Text = " OuterX: " & outerX & " OuterY: " & outerY
    
    
    
    NotFound:
                        ' MathScore = 0
    
                    Next
    
                    Label10.Text = " OuterX: " & outerX
                Next
    Foundit:
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                If ifnd = True Then
    
                    ifnd = True
    
                End If
                ifnd = False
    
    
            Catch
            End Try

    Monday, August 27, 2012 6:08 PM
  • Awesome Job all posters. :)
    Tuesday, February 14, 2017 3:25 PM