Wibs2

Hi,

If I have two bitmaps in memory, say bitmap01 and bitmap02, and I want to compare them to see if they are identical or not, could I do it with a simple If statement, like this

If bitmap01 = bitmap02 Then
MsgBox("Both bitmaps are identical")
Else
MsgBox("The two bitmaps are different")
End If

Have a great weekend

Wibs


Re: Visual Basic Express Edition Comparing two Bitmaps

bruno_1

No it should not work since obviously = is not defined to compare the bitmap. and it should do some comparaison of the object references, and not the visual representation
There is lot about this in other posts like:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=549484&SiteID=1
Comparing pixels in a bitmap

The simplest way to do it, is to make a function that takes the 2 bitmaps as parameters and compare these 2 bitmaps pixel by pixel, and return a boolean...

Hope this helped





Re: Visual Basic Express Edition Comparing two Bitmaps

Wibs2

Thanks bruno, but I thought of a different approach that works really well.

I am actually comparing the pixel data in two image files. I currently have the data in memory in my app in the form of two bitmaps. Instead, I have now loaded the data from the two files into two byte arrays, and the comparison was so easy:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bArray01() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\ba01.raw")
Dim bArray02() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\ba02.raw")
Dim utf As New System.Text.UTF7Encoding()
Dim bString01 As String = utf.GetString(bArray01)
Dim bString02 As String = utf.GetString(bArray02)

If String.Compare(bString01, bString02) = 0 Then
MsgBox("The two files are identical")
Else
MsgBox("The two files are not identical")
End If
End Sub


To test, I created ba01.raw, then copied and pasted it, and renamed the copy to ba02.raw. The comparison above said they were identical. Using a Hex Editor, I then changed the first 00h byte I found to 01h. The compare said the files were different. I put the 01h back to 00h and the compare was identical again. I then did the same test with the first Ascii value I found 42h to 43h, and repeated the test with the first upper value I found, 9Eh to 9Fh.

All tests worked correctly.

I am sure there must be a function to convert a bitmap in memory to a string, but as I already knew how to do this with a byte array I chose this way for expediency.

Wibs





Re: Visual Basic Express Edition Comparing two Bitmaps

bruno_1

You're using some variant of the bits comparison, but with much less coding (No loops and lockbits...). I think this will be much slower than regular bit comparison described in the above 2 articles ( you're doing an encoding to UTF-7, then comparing strings).
But why you used the UTF-7 and not the UTF-8 in your approach





Re: Visual Basic Express Edition Comparing two Bitmaps

Wibs2

Hi Bruno,

Do you have a ballpark for 'much slower' I cannot perceive any time at all in between pressing the button and getting the result. The pixel data consists of 64Kb.

I remember early on in this project of mine I asked for help with a particular problem, and guru1 kindly gave me an a full page of code. amateur1 then responded with 'or he could use just this single command...', to which guru1 replied, 'well the OP never said he wanted it simple!'. Laughed my socks off at that one :-))

I must say I prefer simple code, unless I see any degradation in the performance of my app. Is speed really so important outside of database and video applications I am dealing with graphics, but have yet to see any code slow my app down that you could notice.

Why use UTF-7 over UTF-8 The answer is no particular reason. It seemed wasteful to use UTF-16, so it came down to a 7-bit or 8-bit encoding. I guess I chose UTF-7 because it came before UTF-8 in the list, truth be known, and 7-bit just 'seems' like less work for the cpu than 8-bit. Up to now, a lot of my coding choices have been just gut instinct, and then see what happens. If it works, it stays.

Wibs







Re: Visual Basic Express Edition Comparing two Bitmaps

bruno_1

Yes this is right, i also prefer simple and less coding on "performance". Not from long time, i wanted to do a program similar to Paint, and i had trouble in doing the ghosting for graphics (when you move a line or shape before releasing the button and drawing it), first i thought about using special brushes and caching images...i was thinking about "performance", but finally i realize that i can do everything with 2 lines of code, drawing then refreshing...and everything was fine.

Bruno





Re: Visual Basic Express Edition Comparing two Bitmaps

NateNate

You can use a statement like this:

Code Snippet

if bitmap01.Equals(Bitmap02) then