Results 1 to 3 of 3

Thread: Compare Bitmaps!

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    43

    Question Compare Bitmaps!

    HELLO!
    I have 2 bitmaps (1.bmp and 2.bmp) and i need to know if 1.bmp is the same image as 2.bmp (1.bmp=2.bmp????)

    Any idea?
    Thanks

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Try this, I just wrote it now:
    VB Code:
    1. Private Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
    2. Private Declare Function VarPtr Lib "msvbvm50.dll" (Ptr As Any) As Long
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    4. Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    5. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    6. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    7. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    8. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    9. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    10.  
    11. Private Type SAFEARRAYBOUND
    12.     cElements As Long
    13.     lLbound As Long
    14. End Type
    15.  
    16. Private Type SAFEARRAY2D
    17.     cDims As Integer
    18.     fFeatures As Integer
    19.     cbElements As Long
    20.     cLocks As Long
    21.     pvData As Long
    22.     Bounds(0 To 1) As SAFEARRAYBOUND
    23. End Type
    24.  
    25. Private Type BITMAP
    26.     bmType As Long
    27.     bmWidth As Long
    28.     bmHeight As Long
    29.     bmWidthBytes As Long
    30.     bmPlanes As Integer
    31.     bmBitsPixel As Integer
    32.     bmBits As Long
    33. End Type
    34.  
    35. Private Type OffScreenDC
    36.     DC As Long
    37.     Object As IPictureDisp
    38. End Type
    39.  
    40. Private MyPict1 As OffScreenDC
    41. Private MyPict2 As OffScreenDC
    42.  
    43. Private Sub Form_Unload(Cancel As Integer)
    44.     DeleteDC MyPict1.DC
    45.     DeleteDC MyPict2.DC
    46.     Set MyPict1.Object = Nothing
    47.     Set MyPict2.Object = Nothing
    48. End Sub
    49.  
    50. Private Sub Command1_Click()
    51.     LoadDCs MyPict1, "C:\1.bmp"
    52.     LoadDCs MyPict2, "C:\2.bmp"
    53.     MsgBox CompareDCs(MyPict1, MyPict2)
    54. End Sub
    55.  
    56.  
    57. Private Sub LoadDCs(ByRef MyPict As OffScreenDC, ByVal iFilename As String)
    58.     'Create compatible DC...
    59.     MyPict.DC = CreateCompatibleDC(0)
    60.    
    61.     'Load bitmap...
    62.     Set MyPict.Object = LoadPicture(iFilename)
    63.     'Throw the Picture into the DC...
    64.     SelectObject MyPict.DC, MyPict.Object
    65.    
    66. '    'Paint the image to CHECK its been loaded...
    67. '    BitBlt Picture1.hdc, 0, 0, MyPict.Object.Width, MyPict.Object.Height, MyPict.DC, 0, 0, vbSrcCopy
    68. '    Picture1.Refresh
    69. End Sub
    70.  
    71. Private Function CompareDCs(ByRef MyPict1 As OffScreenDC, ByRef MyPict2 As OffScreenDC) As Boolean
    72. Dim i As Integer
    73. Dim j As Integer
    74. Dim pic1() As Byte
    75. Dim pic2() As Byte
    76. Dim sa1 As SAFEARRAY2D
    77. Dim sa2 As SAFEARRAY2D
    78. Dim bmp1 As BITMAP
    79. Dim bmp2 As BITMAP
    80. Dim sR As Integer, sG As Integer, sB As Integer
    81.  
    82.     'First Pic...
    83.     'Pass the IPictureDisp object to get its details...
    84.     GetObjectAPI MyPict1.Object, Len(bmp1), bmp1
    85.    
    86.     With sa1
    87.         .cbElements = 1
    88.         .cDims = 2
    89.         .Bounds(0).lLbound = 0
    90.         .Bounds(0).cElements = bmp1.bmHeight
    91.         .Bounds(1).lLbound = 0
    92.         .Bounds(1).cElements = bmp1.bmWidthBytes
    93.         .pvData = bmp1.bmBits
    94.     End With
    95.    
    96.     'Links Pic1() to the picture in the DC, any changes
    97.     'made to Pic1() are seen in the picture in the DC...
    98.     CopyMemory ByVal VarPtrArray(pic1), VarPtr(sa1), 4
    99.    
    100.     'Second Pic...
    101.     'Pass the IPictureDisp object to get its details...
    102.     GetObjectAPI MyPict2.Object, Len(bmp2), bmp2
    103.    
    104.     With sa2
    105.         .cbElements = 1
    106.         .cDims = 2
    107.         .Bounds(0).lLbound = 0
    108.         .Bounds(0).cElements = bmp2.bmHeight
    109.         .Bounds(1).lLbound = 0
    110.         .Bounds(1).cElements = bmp2.bmWidthBytes
    111.         .pvData = bmp2.bmBits
    112.     End With
    113.    
    114.     'Links Pic2() to the picture in the DC, any changes
    115.     'made to Pic2() are seen in the picture in the DC...
    116.     CopyMemory ByVal VarPtrArray(pic2), VarPtr(sa2), 4
    117.    
    118.     CompareDCs = True
    119.     For i = 0 To UBound(pic1, 1) - 1 Step 3
    120.         For j = 0 To UBound(pic1, 2)
    121.             sR = pic1(i + 2, j)  'Get Red value for this pixel
    122.             sG = pic1(i + 1, j)  'Get Green value for this pixel
    123.             sB = pic1(i, j)      'Get Blue value for this pixel
    124.             If sR = pic2(i + 2, j) Then
    125.                 'Red Matches
    126.                 If sG = pic2(i + 1, j) Then
    127.                     'Green Matches as well
    128.                     If sB = pic1(i, j) Then
    129.                         'Blue Matches as well
    130.                         '[This Pixel is same color]
    131.                        
    132.                     Else
    133.                         '[Not Same]
    134.                         CompareDCs = False
    135.                     End If
    136.                 Else
    137.                     '[Not Same]
    138.                     CompareDCs = False
    139.                 End If
    140.             Else
    141.                 '[Not Same]
    142.                 CompareDCs = False
    143.             End If
    144.         Next
    145.     Next
    146.    
    147.     'Clear the link...
    148.     CopyMemory ByVal VarPtrArray(pic1), 0&, 4
    149.     CopyMemory ByVal VarPtrArray(pic2), 0&, 4
    150.    
    151. End Function
    Last edited by Electroman; Dec 21st, 2002 at 09:31 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    43
    Thanks for your help!!

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