C# 如何使用字节数组比较两个图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8764280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two images using byte arrays
提问by Diego
I want to be able to convert from Byte[] to Image and vice versa.
我希望能够从 Byte[] 转换为 Image,反之亦然。
I've this two methods from 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));
I get result = false!
我明白了result = false!
Any way to correct this methods or some different functions to achieve my goal?
有什么方法可以纠正这种方法或一些不同的功能来实现我的目标?
Thanks!
谢谢!
采纳答案by Oded
Using ==will compare the object references if not overridden.
==如果没有被覆盖,Using将比较对象引用。
Since these are two differentbyte[]objects, the references are different.
由于这是两个不同的byte[]对象,因此引用是不同的。
You need to compare the byte[]objects item by item in order to confirm that they are identical. You can use SequenceEqualsin this case.
您需要byte[]逐项比较对象以确认它们是否相同。您可以SequenceEquals在这种情况下使用。
回答by Mark Avenius
回答by Hans Ke?ing
When you re-encode an image, the resulting file (or byte array) can be (slightly?) different from the original version. Especially if what you retrieve from the database was a jpeg file!
当您重新编码图像时,生成的文件(或字节数组)可能(略有?)与原始版本不同。特别是如果您从数据库中检索的是 jpeg 文件!
So even if you compare the bytes in the arrays (instead of references) you can get differences.
因此,即使您比较数组中的字节(而不是引用),您也可以获得差异。
EDIT
When you read a byte[] (containing a GIF encoded image) into a BitMap, those bytes are decompressed into 4 (ARGB) bytes per pixel. When you save that BitMap to a (new) gif file (or byte[]), the newly encoded file could be different (for instance, the order in which the colors are stored). So there is no guarantee that the new file (or byte[]) is identical to the old one, although the image itself isn't changed.
编辑
当您将 byte[](包含 GIF 编码图像)读入 a 时BitMap,这些字节将被解压缩为每个像素 4 (ARGB) 字节。当您将该 BitMap 保存到(新)gif 文件(或 byte[])时,新编码的文件可能会有所不同(例如,颜色的存储顺序)。因此无法保证新文件(或 byte[])与旧文件相同,尽管图像本身没有改变。
回答by Tom
I recently needed to write an image cropper that needed to save the fileBytes as an image. here is what I did. Hopefully this will help you.
我最近需要编写一个需要将 fileBytes 保存为图像的图像裁剪器。这是我所做的。希望这会帮助你。
public Image byteArrayToImage(byte[] fileBytes)
{
using (MemoryStream fileStream = new MemoryStream(fileBytes))
{
return Image.FromStream( fileStream );
}
}
obviously my code for the cropping/saving expands upon this. But I was able to return an Image object from the file bytes.
显然,我的裁剪/保存代码对此进行了扩展。但是我能够从文件字节返回一个 Image 对象。

