tags:

views:

1633

answers:

4

Hi,

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

+5  A: 

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);
Filip Ekberg
+4  A: 

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);
 }
Ilya Ryzhenkov
+2  A: 

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];
    }
MusiGenesis
A: 

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