Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCompare two images to find differences
DescriptionThis example shows how to compare two images to find differences in Visual Basic 6.
Keywordsimage, bitmap, compare, difference, imagediff
CategoriesGraphics
 
When you select two images to compare and click the Go button, the program executes the following code. It loads the two image files into hidden PictureBoxes. It finds the smaller of the images' widths and heights, and gives the result PictureBox that size.

Next the program loops over the pixels in the smaller area, comparing the the images' pixels. If two corresponding pixels are equal, the program colors the result pixel white. If the two pixels are unequal, the program makes the result pixel red. When it has examined all of the pixels, the program displays the result image.

The result image highlights differences between the two input images no matter how small the differences are. If a pixel in one image has RGB values 50, 150, 200 and the corresponding pixel in the other image has RGB values 51, 150, 200, the result shows the difference plainly even though you won't be able to tell the difference with your eyes.

 
Private Sub cmdGo_Click()
Dim wid As Integer
Dim hgt As Integer
Dim are_identical As Boolean
Dim x As Integer
Dim y As Integer

    Screen.MousePointer = vbHourglass
    DoEvents

    ' Load the images.
    pic1.Picture = LoadPicture(txtFile1.Text)
    pic2.Picture = LoadPicture(txtFile2.Text)

    ' Size the difference image.
    wid = Min(pic1.ScaleWidth, pic2.ScaleWidth)
    hgt = Min(pic1.ScaleHeight, pic2.ScaleHeight)
    picResult.Width = wid + (picResult.Width - _
        picResult.ScaleWidth)
    picResult.Height = wid + (picResult.Height - _
        picResult.ScaleHeight)

    ' Create the difference image.
    are_identical = True
    For x = 0 To wid - 1
        For y = 0 To hgt - 1
            If pic1.Point(x, y) = pic2.Point(x, y) Then
                picResult.PSet (x, y), vbWhite
            Else
                picResult.PSet (x, y), vbRed
                are_identical = False
            End If
        Next y
    Next x

    If (pic1.ScaleWidth <> pic2.ScaleWidth) Or _
        (pic1.ScaleHeight <> pic2.ScaleHeight) Then _
        are_identical = False
    If are_identical Then
        MsgBox "The images are identical"
    Else
        MsgBox "The images are different"
    End If

    Screen.MousePointer = vbDefault
End Sub
 
For more information on graphics programming in Visual Basic 6, see my acclaimed book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated