在内存为System.Drawing.Image需要MD5哈希为 [英] Need MD5 hash for an in memory System.Drawing.Image

查看:121
本文介绍了在内存为System.Drawing.Image需要MD5哈希为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要MD5哈希一个内存为System.Drawing.Image

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

推荐答案

下面是一个基本的片断。又见@JaredReisinger的注释一些问题。

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());
} 

这篇关于在内存为System.Drawing.Image需要MD5哈希为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆