Comparing Images [duplicate]
I have the exact same image in 2 different objects. They are bmp and when i compare them with .Equals
it returns false.
Is there a reason for this and if so how should images be compared?
If they are two different Image
objects, loaded separately they will have different references.
Image
inherits .Equals
from Object
, not overriding it (see Image
on MSDN).
Since .Equals
does reference equality, it will return false if the references are to different Image
object.
To see if the images are identical, you will have to compare them pixel by pixel/byte by byte.
Equals
:
Determines whether the specified Object is equal to the current Object.
Since these two instances are different object
instances, false
is the correct result.
For an image to truly be compared, you'd need to check byte-for-byte, AFAIK.
.Equals()
checks whether they are references to the same object. If you want to compare them you have to do it manually, comparing the size or the color of all pixels.
By default .Equals(object) comapres reference equality. Meaning that even if you load the same image twice, the simple fact that they are different instances means they will NOT be equal.
Equals methos as you call it is comparing the address of the references ( aka pointers ) and since your objects are probably loaded into two different memory locations the result is false.
You should write your own method to compare the structures at higher level, for example you first compare the height and width and if it's the same then you compare the pixels, probably the Image class could help you anyway but Equals is available from the object class so won't help you.
.Equals generally compares object instances. You need to compare the object "content".
Check out this post: http://www.dreamincode.net/code/snippet2859.htm
Or this: http://www.c-sharpcorner.com/UploadFile/prathore/ImageComparison01022009050404AM/ImageComparison.aspx
精彩评论