Greetings,

If I have an image represented with 256 columns and 256 rows how many bytes used to store such image? and how many pixel in that image?

Please read this and you will understand everything
best example on the internet
Image bits

I'm confused because of this

An image is represented by pixels and one pixel is stored in 4 bytes 1 byte for each of alpha, red, green and blue colors and each of these colors is in range [0 - 255]. So when given an image with 256 columns and 256 rows (width and height) respectively. So the image must be stored in a byte array with size equal 256 X 256 X 4 = 262144

Am I right or wrong in that? as every 4 byte representing 1 pixel

thats correct if talking about argb rgb would be 24 bits in a pixel, in theory you can have as many bits as you want in a pixel.

So why when I tried to store an image with dimensions 1680 X 1050 pixels in a byte array and then tried to read the length of that byte array it did not give me the expected size which, according to my previous comment which you are agree with, should be of size 7056000 byte?!!!

can you post the code and the amount of bytes that it gave you?

Image files also include header information and some of them are compressed.

If you're talking about bitmaps, the exact file format is written on the wiki

Bitmaps are the easiest to read because they are uncompressed.

Greetings,

Here is my code

using System;
using System.IO;
using System.Drawing;

class ByteArrayImage
{
    public static byte[] ImageByteArray(Image imageIn)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        } // end using
    } // end method ImageByteArray

    static void Main(string[] args)
    {
        Image image = Image.FromFile(@"C:\Users\AMR\Documents\visual studio 2012\Projects\ImagetoByteArray\ImagetoByteArray\Resources\2051143-632916_20130820_008.jpg");
        byte[] byteArray = ByteArrayImage.ImageByteArray(image);
        Console.Write("Image properties\nWidth: {0}\nHeight: {1}\nByte array length: {2}\n", image.Width, image.Height, byteArray.Length);
    } // end Main
} // end class ImagetoByteArray

The image dimensions 1680 X 1050 (width and height respectively). The result a byte array with length 703587!!!. However, if my calculations are true the array length must be 7056000 not 703587.

As I mentioned earlier, some files are compressed, JPEG files are one such type, so you will not get a 1:1 mapping out of it as you expect. You will need to look up the JPEG file format if you want to understand it.

This thread is again a proof that in college or any educational institution will teach you something but that something is never applicalbe in real situation.
Lets get back to the topic.
I see you have an image on you hard drive i assume and the file extension is already .JPG. This means that the image has ALREADY been compresed! By that the imagesize (size in bytes) is always smaller. Of course the schema widthheightargb doesnt work here, it works only for .TIFF format the so called highest quality format which does not use compresion! If you right click on your image and select properties and to the math you ve done before you will again see that i does not match! So the size of the .jpg image will always be lower than the size of the .tiff image.If you now go and convert it to .tiff you will get a larger amount of bytes but again not the ideal amount of 7056000. I really don t know how to get to the original image size i mean it certainly possible but fairly complicated. Working with bits and bytes and expecting no loss is unchievable. By the way the whole idea is to get your image as small as possible in terms of bytes(keeping the quality) so i m not sure why you want extra memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.