Reputation: 15886
How can I (as fast as possible) determine if two bitmaps are the same, by value, and not by reference? Is there any fast way of doing it?
What if the comparison doesn't need to be very precise?
Upvotes: 0
Views: 933
Reputation: 273179
You will need a very precise definition of "not very precise".
All the Checksum or Hash methods already posted work for an exact (pixel and bit) match only.
If you want an answer that corresponds to "they look (somewhat) alike" you will need something more complicated.
Upvotes: 2
Reputation: 70369
you can check the dimensions first - and abort the comparison if they differ.
For the comparison itself you can use a variaty of ways:
Upvotes: 6
Reputation: 2801
Try comparing the hashs of the two files
using System;
using System.IO;
using System.Security.Cryptography;
class FileComparer
{
static void Compare()
{
// Create the hashing object.
using (HashAlgorithm hashAlg = HashAlgorithm.Create())
{
using (FileStream fsA = new FileStream("c:\\test.txt", FileMode.Open),
fsB = new FileStream("c:\\test1.txt", FileMode.Open)){
// Calculate the hash for the files.
byte[] hashBytesA = hashAlg.ComputeHash(fsA);
byte[] hashBytesB = hashAlg.ComputeHash(fsB);
// Compare the hashes.
if (BitConverter.ToString(hashBytesA) == BitConverter.ToString(hashBytesB))
{
Console.WriteLine("Files match.");
} else {
Console.WriteLine("No match.");
}
}
}
}
}
Upvotes: 1
Reputation: 1840
You can just use a simple hash like MD5 to determine if their contents hash to the same value.
Upvotes: 2