How to access each byte in a bitmap image
Asked Answered
H

4

3

Say I have a bitmap image, is it possible to iterate through all the individual bytes in the image? If yes, how?

Handyman answered 3/12, 2008 at 15:58 Comment(2)
yes, I guess 1 pixel = 1 byte?Handyman
1 pixel = x bits (where x is the color depth of the image)Royal
M
6

I found this: http://channel9.msdn.com/forums/TechOff/108813-Bitmap-to-byte-array/

Saying that you could use a Memorystream and the .Save method it'd look like this:

System.Drawing.Bitmap bmp = GetTheBitmap();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, stream.Length);
Marinelli answered 3/12, 2008 at 16:3 Comment(0)
B
5

If you need to access the pixel information, the super-slow but super-easy way is to call the GetPixel and SetPixel methods on your Bitmap object.

The super-fast and not-that-hard way is to call the Bitmap's LockBits method and use the BitmapData object returned from it to read and write the Bitmap's byte data directly. You can do this latter part with the Marshal class as in Ilya's example, or you can skip the Marshal overhead like this:

    BitmapData data;
    int x = 0; //or whatever
    int y = 0;
    unsafe
    {
        byte* row = (byte*)data.Scan0 + (y * data.Stride);
        int columnOffset = x * 4;
        byte B = row[columnOffset];
        byte G = row[columnOffset + 1];
        byte R = row[columnOffset + 2];
        byte A = row[columnOffset + 3];
    }
Bilk answered 3/12, 2008 at 16:51 Comment(1)
Thanks for the answer. I suggest that the reader of this answer also reads learn.microsoft.com/en-us/dotnet/csharp/language-reference/…. It was additionally helpful to me.Photooffset
L
4

Use LockBits member on Bitmap class to obtain BitmapData, then use Scan0 and Marshal.ReadByte to readbytes. Here is small example (it is not about correct brightness adjustment, though):

    public static void AdjustBrightness(Bitmap image, int brightness)
    {
        int offset = 0;
        brightness = (brightness * 255) / 100;
        // GDI+ still lies to us - the return format is BGR, NOT RGB.
        BitmapData bmData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        int stride = bmData.Stride;
        IntPtr Scan0 = bmData.Scan0;

        int nVal = 0;
        int nOffset = stride - image.Width * 3;
        int nWidth = image.Width * 3;

        for (int y = 0; y < image.Height; ++y)
        {
            for (int x = 0; x < nWidth; ++x)
            {
                nVal = Marshal.ReadByte(Scan0, offset) + brightness;

                if (nVal < 0)
                    nVal = 0;
                if (nVal > 255)
                    nVal = 255;

                Marshal.WriteByte(Scan0, offset, (byte)nVal);
                ++offset;
            }
            offset += nOffset;
        }
        image.UnlockBits(bmData);
    }
Livi answered 3/12, 2008 at 16:8 Comment(0)
B
0

Another solution is to use LockBits and Marshal.Copy to convert your bitmap into an array. I needed this solution because I had two images that differed only in their color depth and the other proffered solutions don't handle that well (or are too slow).

using (Bitmap bmp = new Bitmap(fname)) {
    // Convert image to int32 array with each int being one pixel
    int cnt = bmp.Width * bmp.Height * 4 / 4;
    BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                            ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    Int32[] rgbValues = new Int32[cnt];

    // Copy the RGB values into the array.
    System.Runtime.InteropServices.Marshal.Copy(bmData.Scan0, rgbValues, 0, cnt);
    bmp.UnlockBits(bmData);
    for (int i = 0; i < cnt; ++i) {
        if (rgbValues[i] == 0xFFFF0000)
            Console.WriteLine ("Red byte");
    }
}
Brieta answered 11/5, 2009 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.