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.
Printable View
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.
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 :)
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.
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?
Significantly faster, but file IO would be faster still I should think.
VB Code:
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Private Function arePicturesEqual(ByVal pBox1 As PictureBox, pBox2 As PictureBox) As Boolean arePicturesEqual = True: Dim hDc1 As Long, hDc2 As Long Dim i As Long, j As Long: pBox1.ScaleMode = 3: pBox2.ScaleMode = 3 hDc1 = pBox1.hdc: hDc2 = pBox2.hdc: For i = 1 To pBox1.ScaleWidth For j = 1 To pBox2.ScaleHeight If Not GetPixel(hDc1, i, j) = GetPixel(hDc2, i, j) Then arePicturesEqual = False Exit Function Next Next Next End Function
Pass both pictureboxes to this function as parameters.
I haven't tested it, but it should work :)
Very nice, thanks :D