在内存System.Drawing.Image中需要MD5哈希

Ron*_*rby 7 .net c# hash md5 image

在内存System.Drawing.Image中需要MD5哈希

µBi*_*Bio 6

这是一个基本的片段.有关一些问题,另请参阅@JaredReisinger的评论.

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());
} 
Run Code Online (Sandbox Code Playgroud)