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!

2
  • same problem here: stackoverflow.com/questions/8763630/… except the == stuff use pic.equals(imageToByteArray(byteArrayToImage(pic)); Commented Jan 6, 2012 at 21:05
  • @OliverBernhardt try this code new byte[] { 1 }.Equals(new byte[] { 1 }) Commented Jan 6, 2012 at 21:12

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Additionally, the OP should read something like stackoverflow.com/questions/649444/… to get an idea of how to properly compare them.
@ChrisShain - The question is about having the same items in different arrays - ordering is not important there. It is very important here.
@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.
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.

2 Comments

then why this returns false? new byte[] { 1 }.Equals(new byte[] { 1 })
@L.B: very true. Forgive me; for arrays, this does not work. See here for how to compare byte arrays properly. Updating my answer accordingly.
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.

Comments

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.

1 Comment

Why would that be?? Is there any way to avoid it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.