Resolved ImageComparer Class

cbreemer

Well-known member
Joined
Dec 1, 2021
Messages
184
Programming Experience
10+
I hope this is the correct forum for this question - I'm always a bit confused about all the various forums here !

Has anybody here ever used the ImageComparer class ? It is documented (albeit extremely rudimentary) in this page
ImageComparer Class (Microsoft.VisualStudio.TestTools.UITesting)
but I can't get VS2022 to see the class whatever I try. I'm starting to doubt whether it (still) exists in the current .Net and VS2022.

I'd probably have saved time by just writing a method that compares the bitmaps... but I'm peeved that there seems to be no such functionality available.
 
Did you add a reference to the "Microsoft.VisualStudio.TestTools.UITesting.dll" assembly that contains that class?
 
Did you add a reference to the "Microsoft.VisualStudio.TestTools.UITesting.dll" assembly that contains that class?
I had tried to do that in the Reference Manager, but could not find this particular assembly with the Search function. Only an assembly with a subtly different name but that did not do the trick. I was kind of assuming the Search option would find it. But as per your suggestion I located that dll and copied the path in Browse, and bingo, all is well ! I guess I should have thought of that myself but I'm still relatively new to this stuff. Thanks for your help !
 
Having got it to work, I decided not to use it... It apparently compares each and every pixel of the images which takes a long time and seems overkill for my purpose. I thought just comparing 1000 pixels along the length of one diagonal was probably sufficient.
 
Why do you need to compare images?
 
Why do you need to compare images?
When I copy files from my phone to my pc, I give them a standardized (and hopefully unique) name yyyyMMdd_HHmmss.jpg. Sometimes a file of that name already exists. In most cases it is the same image, in which case I can skip it, but annoyingly, it can also be a different image (like when I'd copied it from my wife's smartphone) in which case I need to save with a different name. And to make this decision I need to compare the images. The kludgy compare I devised seems good enough to do this, and is lightning fast compared to ImageComparer which needs to compare millions of pixels.
 
What not check the file sizes and if the sizes match compute the file hash and compare hashes?
 
What not check the file sizes and if the sizes match compute the file hash and compare hashes?
That is exactly what I had implemented to start with ! But it did not fly, as the file on the computer always has EXIF data, and the file on the phone sometimes hasn't (when it 's a WhatsApp image). So the sizes don't always match for the same images.
 
So you need to pull out just the image bytes, then do the hash on that. Although it sounds daunting, it is likely still faster than loading, decompressing, rendering, and then comparing pixels.
 
So you need to pull out just the image bytes, then do the hash on that. Although it sounds daunting, it is likely still faster than loading, decompressing, rendering, and then comparing pixels.
Dang, I had not thought of that variant ! Although to pull out just the image bytes (i.e. remove the exif data) I think I still have to load it into Image object, or do you have another idea ? Not that a different solution is really needed, performance-wise. This is already about 1000 times faster than ImageComparer, and quite negligible compared to the time needed for the actual copy from phone to pc.

BTW - I appreciate you thinking along with me, and with everybody else on this forum👍
 
Back
Top Bottom