0

I am using BinaryReader to read the bytes of an image, I am having some issues trying to read the ARGB values of a bitmap image using BinaryReader. Can anyone suggest a way I could get the byte value for each pixel in a bitmap image?

Thanks in advance

Conor Shannon
  • 439
  • 2
  • 6
  • 17
  • Easy way: use GetPixel(x, y) method of Bitmap (slow), read bitmap using pointers (advanced, but fast) – Aurimas Neverauskas Nov 13 '14 at 15:00
  • Do you know of a way of using the BinaryReader and not the bitmap functions? – Conor Shannon Nov 13 '14 at 15:03
  • http://www.codeproject.com/Questions/308076/Read-bitmap-file-using-binary-reader-in-csharp please read 2nd solution – Aurimas Neverauskas Nov 13 '14 at 15:05
  • "I am having some issues trying to read the ARGB values of a bitmap image using BinaryReader." What code do you have so far? – IS4 Nov 13 '14 at 15:13
  • @IllidanS4 lol, Bitmap file format is not plain format containing only pixels. It contains lots of metadata like format, bpp, size etc, so you should ignore that 'junk' data and start reading at actual data, but that's impossible without knowing actual .bmp file structure. – Aurimas Neverauskas Nov 13 '14 at 15:22
  • @AurimasNeverauskas Have I evoked the impression that I didn't know that? – IS4 Nov 13 '14 at 15:28
  • @IllidanS4 yes, you did, because you said that you're having issues when doing it this way. Is it homework to do it this way? – Aurimas Neverauskas Nov 13 '14 at 15:43
  • @AurimasNeverauskas I am not the questioner, I was just asking for more information about the question. – IS4 Nov 13 '14 at 17:35

3 Answers3

1

Easy way is to use unsafe context and lock some bits. Oversimplified sample:

unsafe
{
    var bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

    byte* first = (byte*)bitmapData.Scan0;   
    byte a = first[0];
    byte r = first[1];
    byte g = first[2];
    byte b = first[3];

    ...

    bmp.UnlockBits(bitmapData);
}

However, if you still need to use BinaryReader and you know how many bytes per pixel there are, It is possible to just skip the header (you can find it's length in @Bradley_Ufffner 's link) and access the bytes.

Bart Juriewicz
  • 579
  • 7
  • 19
0

You will need to study the BMP file format available here: http://en.wikipedia.org/wiki/BMP_file_format Reading the file correctly will involve figuring out the pixel format from the header and parsing the data correctly based on that. The file may be palatalized, in which case you will need to read out the color table data and use it to map pixels to actual colors. Pixel data may also be compressed and will have to be extracted based on values in the header.

This won't be a simple project, things like this are the reason graphics libraries were invented.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
0

If you need to read a bitmap's pixel data using BinaryReader, try UnmanagedMemoryStream:

Bitmap bmp = new Bitmap("img.bmp");
var bits = bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
try{
    unsafe{
        using(Stream bmpstream = new UnmanagedMemoryStream((byte*)bits.Scan0, bits.Height*bits.Stride))
        {
            BinaryReader reader = new BinaryReader(bmpstream);
            for(int y = 0; y < bits.Height; y++)
            {
                bmpstream.Seek(bits.Stride*y, SeekOrigin.Begin);
                for(int x = 0; x < bits.Width; x++)
                {
                    byte b = reader.ReadByte();
                    byte g = reader.ReadByte();
                    byte r = reader.ReadByte();
                    byte a = reader.ReadByte();
                }
            }
        }
    }
}finally{
    bmp.UnlockBits(bits);
}
IS4
  • 11,945
  • 2
  • 47
  • 86