如何使用字节数组比较两个图像 [英] How to compare two images using byte arrays
问题描述
我希望能够从 Byte[] 转换为 Image,反之亦然.
I want to be able to convert from Byte[] to Image and vice versa.
我有这两种方法来自 此处:
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));
我得到result = false
!
有什么方法可以纠正这种方法或一些不同的功能来实现我的目标?
Any way to correct this methods or some different functions to achieve my goal?
谢谢!
推荐答案
如果没有被覆盖,使用 ==
将比较对象引用.
Using ==
will compare the object references if not overridden.
因为这是两个不同的 byte[]
对象,所以引用是不同的.
Since these are two different byte[]
objects, the references are different.
您需要逐项比较 byte[]
对象以确认它们是否相同.在这种情况下,您可以使用 SequenceEquals
.
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.
这篇关于如何使用字节数组比较两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!