How to access each byte in a bitmap image

北城以北 提交于 2019-11-30 15:59:14

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);

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];
    }

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);
    }

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");
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!