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!
谢谢!
4 个解决方案
#1
15
Using ==
will compare the object references if not overridden.
如果不重写,使用==将比较对象引用。
Since these are two different byte[]
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 SequenceEquals
in this case.
您需要逐项比较byte []对象,以确认它们是相同的。在这种情况下,您可以使用SequenceEquals。
#2
2
==
means that you have a reference to the same object in memory.
==表示您对内存中的同一对象有引用。
This shows how to compare byte arrays in a few different ways.
这显示了如何以几种不同的方式比较字节数组。
#3
2
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对象。
#4
1
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.
编辑当您将一个字节[](包含GIF编码图像)读入BitMap时,这些字节将被解压缩为每个像素4个(ARGB)字节。将BitMap保存到(新)gif文件(或byte [])时,新编码的文件可能不同(例如,存储颜色的顺序)。因此,无法保证新文件(或byte [])与旧文件相同,尽管图像本身不会更改。
#1
15
Using ==
will compare the object references if not overridden.
如果不重写,使用==将比较对象引用。
Since these are two different byte[]
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 SequenceEquals
in this case.
您需要逐项比较byte []对象,以确认它们是相同的。在这种情况下,您可以使用SequenceEquals。
#2
2
==
means that you have a reference to the same object in memory.
==表示您对内存中的同一对象有引用。
This shows how to compare byte arrays in a few different ways.
这显示了如何以几种不同的方式比较字节数组。
#3
2
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对象。
#4
1
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.
编辑当您将一个字节[](包含GIF编码图像)读入BitMap时,这些字节将被解压缩为每个像素4个(ARGB)字节。将BitMap保存到(新)gif文件(或byte [])时,新编码的文件可能不同(例如,存储颜色的顺序)。因此,无法保证新文件(或byte [])与旧文件相同,尽管图像本身不会更改。