17

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!

Community
  • 1
  • 1
Diego
  • 16,436
  • 26
  • 84
  • 136
  • same problem here: http://stackoverflow.com/questions/8763630/c-sharp-gif-image-to-memorystream-and-back-lose-animation except the == stuff use pic.equals(imageToByteArray(byteArrayToImage(pic)); – Oliver Bernhardt Jan 06 '12 at 21:05
  • @OliverBernhardt try this code `new byte[] { 1 }.Equals(new byte[] { 1 })` – L.B Jan 06 '12 at 21:12

4 Answers4

16

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Additionally, the OP should read something like http://stackoverflow.com/questions/649444/testing-equality-of-arrays-in-c-sharp to get an idea of how to properly compare them. – Chris Shain Jan 06 '12 at 20:51
  • 2
    @ChrisShain - The question is about having the same items in different arrays - ordering is not important there. It is _very_ important here. – Oded Jan 06 '12 at 20:55
  • @Oded Very true, I hadn't caught that. There are tons of good questions on the subject though, this one was just a (bad) example. – Chris Shain Jan 06 '12 at 20:58
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.

Community
  • 1
  • 1
Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
  • then why this returns false? `new byte[] { 1 }.Equals(new byte[] { 1 })` – L.B Jan 06 '12 at 21:04
  • @L.B: very true. Forgive me; for arrays, this does not work. See [here](http://stackoverflow.com/questions/43289/comparing-two-byte-arrays-in-net) for how to compare byte arrays properly. Updating my answer accordingly. – Mark Avenius Jan 06 '12 at 21:09
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.

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.

Tom
  • 1,047
  • 3
  • 24
  • 44
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!

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.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111