Well, actually it took me quite a while to notice this line:
if (compare(image2, image2))
You are passing the same Bitmap
to the function compare
, so when the function attempts to call LockBits
on the same Bitmap
twice, this exception occurs.
So in order to fix it, don't pass the same bitmaps to the function.