-1

What is the need/purpose of converting an image to a byte array?

Why do we need to do this?

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Mike
  • 7
  • 2
  • 6
    That depends on what you want to accomplish, doesn't it ? – driis Oct 25 '10 at 17:39
  • 3
    Your question is too vague. There are plenty of different resons in different situations. You should specify in what situation you are needing this, or you will just get a lot of guesses about what you are doing, and a lot of answers for other slightly related questions that people think that you are really wanting to ask about (like for example *how* to convert an image to a byte array). – Guffa Oct 25 '10 at 17:46
  • Seems like a homework question. If it is, tag it. At the moment this isn't even relevant to C#. I removed C# from your post. If it actually is a c# specific question, please update your post with something that actually has to do with C#. – Mike Atlas Oct 25 '10 at 18:48
  • 1
    Last time I checked, a image was byte array in the first place. Terrible question. – Claus Jørgensen Oct 25 '10 at 18:52
  • This could do with being deleted. – halfer Jul 18 '19 at 20:18

9 Answers9

5

What's the purpose of converting an Image to a byte array?

  • Saving an image to disk

  • Serializing the image to send to a web service

  • Saving the image to a database

  • Serializing the image for processing

...are just a few that I can think of off the top of my head.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
4

I think the most common use would be saving an image in a database (BINARY(MAX) data type).

Brad
  • 15,361
  • 6
  • 36
  • 57
2

Most common is to persist the image in the database as a blob (binary large object) but it could also be used in web services where you could take in a byte array as a method argument and then convert that to an image for whatever reason.

Chris Conway
  • 16,269
  • 23
  • 96
  • 113
1

the only time i've ever needed to do this was to compare two images pixel-by-pixel to see if they were identical (as part of an automated test suite). converting to bytes and pinning the memory allowed me to use an unsafe block in C# to do a direct pointer-based comparison, which was orders of magnitude faster than GetPixel.

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
1

Recently I wrote a code to get hash from image, here is how:

    private ImageConverter c = new ImageConverter();
    private byte[] getBitmapHash(Bitmap hc) {
        return md5.ComputeHash(c.ConvertTo(hc, typeof(byte[])) as byte[]);
    }

Here is this in context. Serializing image or adding it to database in raw byte format (without mime type) is not something that seems to be sensible, but you can do that. Image processing and cryptography are most likely of places where this is useful.

Community
  • 1
  • 1
Margus
  • 19,694
  • 14
  • 55
  • 103
0

To further generalize what Brad said: Serialization and (probably) a basis for object comparison.

TrevorB
  • 257
  • 1
  • 3
  • 10
0

Also is helpful if you have a image in memory and want to send it to someone via a protocol (HTTP for example). A perfect example would be the method "AddBytesForUpload" in the Chilkat HTTPRequest clas [http://www.chilkatsoft.com/refdoc/vbnetHttpRequestRef.html].

Why would you ever need to do this you may ask... Well lets assume we have a directory of images we want to auto upload to imageshack, but do some mods before hand like put our domain name at the bottom right. With this, u load the image in memory, do the mods with it that u need, then simply attach that stream to the HTTPRequest object. Without the array u would need to then same to a file before uploading, which in turn will create either a new file u then need to delete after, or over write the original, which is not always ideal.

Anthony Greco
  • 2,885
  • 4
  • 27
  • 39
-1
public byte[] imageToByteArray(System.Drawing.Image image)
{
 MemoryStream ms = new MemoryStream();
 image.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return ms.ToArray();
}
Georgi
  • 395
  • 3
  • 4
  • Thanks, but it's not what i'm asking... i'm asking, why we have to do this conversion? – Mike Oct 25 '10 at 17:43
-2

If it is a image file use File.ReadAllBytes().

crauscher
  • 6,528
  • 14
  • 59
  • 85