Created On:  24 September 2010

Problem:

Is it possible to verify that two bitmaps match using SilkTest Workbench?

Resolution:

SilkTest Workbench does not provide an in-built function for verifying that 2 bitmaps are the same. However, we can leverage against the Bitmap class within the System.Drawing namespace to build our own function that will perform the required functionality. The function below highlights how we can verify that two bitmaps stored on disk are the same:


      Public Function compareBitmaps(source As String, compare As String) As Boolean

           
            Dim srceBmp As New Bitmap(source)
            Dim compBmp As New Bitmap(compare)
           
            Dim srceHeight, srceWidth As Integer
            Dim compHeight, compWidth As Integer
           
            'Get Height Width for source bitmap
            srceHeight = srceBmp.Height
            srceWidth = srceBmp.Width
     
            'Get Height Width for comparison bitmap
            compHeight = compBmp.Height
            compWidth = compBmp.Width
           
           
            'If the values are different the images are different
            If(srceHeight <> compHeight Or srceWidth <> compWidth)
                  Return False
            End If
           
     
            'Check the pixels match in each image
            Dim width, height As Integer
            width = 0
            height = 0
           
            For height=0 To srceHeight-1
                  For width=0 To srceWidth-1
                        If srceBmp.GetPixel(width,height) <> compBmp.GetPixel(width,height)
                              Return False
                        End If
                  Next width 
            Next height
           
           
            Return True
      End Function


Using the above function as the base function for comparing bitmaps, we can create a verifyBitmap function similar to that in SilkTest Classic. The function below accepts a TestObject and a string representing the base bitmap to which a controls visual appearance must be compared:

      Public Function verifyBitmap(wObj As TestObject, sComp As String) As Boolean
            Dim sTemp As String
            'Store a bitmap of the TestObject in the users temp directory
           
sTemp = System.IO.Path.GetTempPath()+
"\verifyBMP.bmp"

           
wObj.CaptureBitmap(sTemp)

            'compre the temporary image to the base image and return the result
           
Return
compareBitmaps(sTemp, sComp)
      End Function