Results 1 to 7 of 7

Thread: Compare two Pictures

  1. #1

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247

    Compare two Pictures

    Can anyone tell me if there is some API or a good method for checking if two pictures (in Picture Boxes) are the same? I already know how to check pixel by pixel, but i was looking for a faster and/or easier way.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well checking pixel by pixel isn't difficult.
    Very easy actually.

    And its not all that slow if you're using GetPixel and SetPixel.
    Failing that however, you could use SavePicture, and then compare the two files with File I/O
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    i would say save them to binary temp file, open them into a byte array, and then it is very easy. Check to see if their Upper bounds, or higher indexes are the same. If so, go from 0 to the Upper bound using

    For Index = 0 to UBOUND(Bitmap1)
    If Bitmap1(Index)<>Bitmap2(Index) then NotEqual
    Next Index

    That should be about the fastest way imaginable.

  4. #4

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    Yeah, im not very good at File I/O. I just understand it a little bit, but ill play with those ideas.

    The method i was using now was

    for x = 1 to pic1.width
    for y = 1 to pic2.height
    if pic1.point(x,y) <> pic2.point(x,y) then
    msgbox ("they are not equal")
    exit sub
    next y
    next x

    msgbox ("they are equal")

    -------------------------------

    Is GetPixel faster?

  5. #5
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Significantly faster, but file IO would be faster still I should think.
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    2.  
    3. Private Function arePicturesEqual(ByVal pBox1 As PictureBox, pBox2 As PictureBox) As Boolean
    4.     arePicturesEqual = True: Dim hDc1 As Long, hDc2 As Long
    5.     Dim i As Long, j As Long: pBox1.ScaleMode = 3: pBox2.ScaleMode = 3
    6.     hDc1 = pBox1.hdc: hDc2 = pBox2.hdc:
    7.     For i = 1 To pBox1.ScaleWidth
    8.         For j = 1 To pBox2.ScaleHeight
    9.             If Not GetPixel(hDc1, i, j) = GetPixel(hDc2, i, j) Then
    10.                 arePicturesEqual = False
    11.                 Exit Function
    12.             Next
    13.         Next
    14.     Next
    15. End Function

    Pass both pictureboxes to this function as parameters.
    I haven't tested it, but it should work
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    Very nice, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width