Need MD5 hash for an in memory System.Drawing.Image

前端 未结 3 1737
难免孤独
难免孤独 2020-12-18 01:42

Need MD5 hash for an in memory System.Drawing.Image

3条回答
  •  春和景丽
    2020-12-18 02:09

    Here is a basic snippet. See also @JaredReisinger 's comment for some questions.

    using System.Security.Cryptography;
    using System.Text;
    using System.Drawing.Imaging;
    // ...
    
    // get the bytes from the image
    byte[] bytes = null;
    using( MemoryStream ms = new MemoryStream() )
    {
        image.Save(ms,ImageFormat.Gif); // gif for example
        bytes =  ms.ToArray();
    }
    
    // hash the bytes
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] hash = md5.ComputeHash(bytes);
    
    // make a hex string of the hash for display or whatever
    StringBuilder sb = new StringBuilder();
    foreach (byte b in hash)
    {
       sb.Append(b.ToString("x2").ToLower());
    } 
    

提交回复
热议问题