Freshblood
Freshblood

Reputation: 6431

Why Images are represented by byte array?

I was reading Scott Guthrie blog and i have encounter a sample. You can find it in Here

It is used byte array for represent Picture in that sample. I have no idea why it is represented by byte array. I also remember that some of files are processed as byte array in .NET Framework too.

Can someone explain to me why byte array instead of string or even a class which holds picture ?

Upvotes: 1

Views: 5343

Answers (3)

Oded
Oded

Reputation: 499002

Images are binary data - this is easily represented as byte arrays.

The image in the sample is stored in the database as a BLOB - not a string or location, that is, it is binary data.

Upvotes: 5

Guffa
Guffa

Reputation: 700322

A collection of bytes is the simplest way to represent an image file as data. A string would not be suitable as it contains character codes, and an image file does not consist of characters. There is no special class for holding the data of an image file, as a byte array works just fine for that.

Any file can be treated as a collection of bytes, so a byte array is the result of reading a file as binary data. A file can also be decoded as a specific format, like a text file which results in a string, or a compressed image format (JPEG, GIF, PNG et.c.) which results in a Bitmap object containing the decompressed image.

Upvotes: 3

Stephane Rolland
Stephane Rolland

Reputation: 39906

One handles a bitmap as an array of RGB value. Fear each pixel, for example, each value R, G and B is coded in one byte. You could also have a fourth value which is transparency.

Playing with the colour/transparency index, the x coordinate of the pixel, and the image width for the getting the pixels at y coordinate... thus one has quick access to the whole image, at the cheap price of some multiplication and a array operator[] call.

Nowadays, efficiency may still be an issue when dealing with images (lots and lots of working on each and every pixel, which means lots and lots of loops all over the content of the image ).

That could explain that images are still handled through arrays of bytes.

Upvotes: 1

Related Questions