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));
I get result = false
!
Any way to correct this methods or some different functions to achieve my goal?
Thanks!
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.
==
means that you have a reference to the same object in memory.
This shows how to compare byte arrays in a few different ways.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With