Question

I work on bitmap i read header of bmp 24 bit per pixel ,width 320 height 240,offset start of image data in location 54,use c#

in location 54 in bmp array i access to data ,i store data in new array :

       struct pix  //structure for pixel in bmp image
       {
         public byte r;//Red
         public byte g;//Green
         public byte b;//Blue
        }; 


        Bitmap img = new Bitmap(opendialog1.FileName);

        string filename = opendialog1.FileName; 
        byte[] bmp = File.ReadAllBytes(filename); 

        int i=54;

        pix[ , ] bmpdata = new pix[img.Height, img.Width];  //create array of structure

        for (int row = 0; row < img.Height; row++)
        {
            for (int col = 0; col < img.Width; col++)
            {
                bmpdata[row, col].r = bmp[i];
                bmpdata[row, col].g = bmp[i + 1];
                bmpdata[row, col].b = bmp[i + 2];
                i += 3;
            }

is this correct method for copy data from bmp array into new array for data only ,i use c# window form ?

Was it helpful?

Solution

No this is not how you get the pixel data (color information).

Compare it yourself with the color defined at each pixel by calling GetPixel(x,y)

Also you could compare with this method:

        var pixels = new Color[img.Height*img.Width];
        for (int row = 0; row < img.Height; row++)
        {
            for (int col = 0; col < img.Width; col++)
            {
                pixels[row + col] = img.GetPixel(col, row);
            }
        }

If you just want the byte data, use img.Save() to save to a new MemoryStream for example...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top