Align two identical images with different file names

If one image has been saved twice with two different file names, is there a way to compare them to make sure they are the same ..?

I hope a basic hash or CRC type check can work.?

There may not be a file size because there are millions of images in the pool, and different images may have the same size.

Hope this is an easy way to do this in C # ..

+3
source share
5 answers

If the contents of the file are identical, then the cryptographic hash will at least give a very good idea of ​​equality. The class SHA256would be a good candidate here, although it may be just above the top. For instance:

static byte[] Sha256HashFile(string file)
{
    using (SHA256 sha256 = SHA256.Create())
    {
        using (Stream input = File.OpenRead(file))
        {
            return sha256.ComputeHash(input);
        }
    }
}

- , , Convert.ToBase64, . , :) Enumerable.SequenceEqual:

byte[] hash1 = Sha256HashFile("file1.png");
byte[] hash2 = Sha256HashFile("file2.png");
bool same = hash1.SequenceEqual(hash2);    

, IEqualityComparer<byte[]>, , , base64. , :

var hashToNameMap = new Dictionary<string, string>();
foreach (string file in files)
{
    byte[] hash = Sha256HashFile(file);
    string base64 = Convert.ToBase64(hash);
    string existingName;
    if (hashToNameMap.TryGetValue(base64, out existingName))
    {
        Console.WriteLine("{0} is a duplicate of {1}", file, existingName);
    }
    else
    {
        hashToNameMap[base64] = file;
    }
}

:

  • , , .
  • - (, , ). .
  • , , ... , , , .

, , , .. , - , , , , , , .

+7

-, . , .

. , , . , :

  • . ( > 100 ), -, . kB , , , , . 512 . Jpeg- Hash: .

  • . xor.

  • , Hash, . , .

  • , Hash (Size, Hash).
+4

System.Security.Cryptography;

SHA1

using(SHA1 sha = SHA1.Create()) { //added using based on Jon Skeet comment
    byte[] newData = sha.ComputeHash(data);
}

- []

newData -

, , , , ( , )

+1

, . .

.

0

-

public string ImageToBase64(Image image, 
                            System.Drawing.Imaging.ImageFormat format)
{ 
    using (MemoryStream ms = new MemoryStream())
    { 
         // Convert Image to byte[]
         image.Save(ms, format);  
         byte[] imageBytes = ms.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String; 
    }
}

then you can just do it String.Compare(). This is probably a slow process for large images because it generates a rather large string, but I posted it just for the sake of completion :)

0
source

All Articles