-
A quick question (hopefully:
I've got two bitmaps, one stored to disk, and one that's put into a picturebox using bitblt. I'd like to compare the two and check if they're identical.
Is there a simple way to do this? Er, even a complex one would be good. Right now, I'm checking a variety of pixel locations to see if certain colors match, but it's rather tedious with many images.
Ideas?
- Kronix
-
Yep, declare GetPixel. Also have 2 picboxes, 1 with the pic in it (autoresize), and 1 blank with autoresize. Also have the file path (full path) stored in MyFile. Okey... here we go...
Also, just add the code that is INBETWEEN the Sub, End Sub lines, except for user-made functions.
Code:
'add this code to FORM LOAD:
Sub
Dim RET As Boolean
Picture2.Picture = LoadPicture(MyPath) 'loads the file to blank picbox
Picture1.ScaleMode = vbPixels 'set the scalemode to 3
RET = Compare(Picture1.hDC, Picture2.hDC,Picture1.ScaleHeight,Picture1.ScaleWidth) 'compare the pics and store result in ret
End Sub
Function Compare(ShDC,DhDC,Width, Height) as Boolean
Dim I, J
Dim RES1 As Long
Dim RES2 As Long
For J = 0 to Height
For I = 0 to Width
RES1 = Getpixel(ShDC,I,J): RES2 = GetPixel(DhDC,I,J)
If RES1 = RES2 Then
Else
Compare = FALSE
Exit For: Exit For: Exit Function
End If
Next I
Next J
Compare=TRUE
End Function
-
the alternative for performance would be DMA, which you could learn at http://www.ur.co.nz/
you should use long arrays instead of byte arrays though and operations will be 4 times faster, you xor both arrays for each element and or the results. To make this loop short circuit, you exit when the or result converted to boolean. Another alternative would be to bitblt with vbPatInvert (same as xor) take out a byte array and convert it to a unicode string, and then just simply compare it with an empty string (filled with nullchars)
-
hehe.... :(
That's kedaman for you, always finding out the BETTER alternative. :)
Use kedaman's advice, I'm sure his'll work better (and faster!!)
-
Chuckle. I was doing something roughly equivalent to Sastraxi's idea, and was hoping for something faster.
I do appreciate your replies...
However, I'm not sure I follow Kedaman's post completely. Here's what I got: XOR both the pics, then OR the results together
Er, then you talk about shorting the loop. Do I do this repeatedly, then? And how does this become a Boolean result?
As for the 'other alternative', I don't think I quite understand. Do I XOR the two together? Taking out a byte array and converting to unicode?
Sorry for being rather dense...
- Kronix the Befuddled
-
aaah, i realize this always happens to me, i look at a post, then replies without thinking. Xor + Or is the long way round, youre not doing bitwise comparation anyway so you could just convert both bitmaps into strings and then compare them, that's the simple way but i'm not sure if it's good at performance. Bitwise Xor is the same as Not Eqv. and that would be the same as not = for numerical comparation, so we could just skip bitwise and try arithmetic equivalence instead.
Picking any samples of source with safe arrays at unlimited realities (go for the link and look under tutorials)
Code:
Function eq(array1&(),array2&()) as boolean
c=ubound(array1)
if c<>UBound(array2) then exit function
for x=0 to c
if array1(x)=array2(x) then exit for
next x
eq=x=c+1
end function
and have single dimensioned long arrays instead of 2d byte arrays.
eq
-
Sas, did you check that code, you posted? I may be
wrong, but i think in your If..Then..Else, you will never
hit the "Exit Function". You Exit the For first, which
automatically jumps to the next line after your loop
(back into your J loop). Your function will ALWAYS return
true.
Z.
-
Yeah, Zaei, I think you're right... I was just trying to be 'clean' about exiting, I guess I shouldnt've done that. Yeah, just remove the two exit fors and you should be on your way.