问题描述
我希望能够从字节[]转换为图像,反之亦然。
I want to be able to convert from Byte[] to Image and vice versa.
我已经从<一个这两种方法href=\"http://www.$c$cproject.com/KB/recipes/ImageConverter.aspx?fid=337686&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick\">here:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
他们似乎工作,但如果我做的:
They seem to work, but if I do:
byte[] pic = GetImageFromDb();
bool result = pic == imageToByteArray(byteArrayToImage(pic));
我得到结果=假
!
要纠正这个方法还是有些不同的功能来实现我的目标什么办法?
Any way to correct this methods or some different functions to achieve my goal?
谢谢!
推荐答案
使用 ==
如果没有覆盖将比较对象的引用。
Using ==
will compare the object references if not overridden.
由于这是两个的不同的的字节[]
对象引用是不同的。
Since these are two different byte[]
objects, the references are different.
您需要比较字节[]
逐项为了反对项目,以确认它们是相同的。您可以在此情况下,使用
You need to compare the byte[]
objects item by item in order to confirm that they are identical. You can use SequenceEquals
in this case.
这篇关于如何使用字节数组来比较两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!