How to set pixel into bitmap with Pixel format of Format8bppIndexed ? [modified]
-
I define new bitmap in my code Bitmap image = new Bitmap( width, height, PixelFormat.Format8bppIndexed); ==> that mean that this bitmap is gray scale color. Now, i want to define what color will be at place (i, j) so i usually use ( on 24 color bit ) the method 'SetPixel( i, j, Color.FromRGB( ... ) )' But in this case that the bitmap is on gray scale i cant do it because i have only one value of the color ... and the application crash all the time when i try to call Color.FromRGB(theOneColorIhave) Someone know other way to do set the pixel values - some way that can be use with 8 bit format ? Thanks
modified on Wednesday, November 3, 2010 10:36 AM
-
I define new bitmap in my code Bitmap image = new Bitmap( width, height, PixelFormat.Format8bppIndexed); ==> that mean that this bitmap is gray scale color. Now, i want to define what color will be at place (i, j) so i usually use ( on 24 color bit ) the method 'SetPixel( i, j, Color.FromRGB( ... ) )' But in this case that the bitmap is on gray scale i cant do it because i have only one value of the color ... and the application crash all the time when i try to call Color.FromRGB(theOneColorIhave) Someone know other way to do set the pixel values - some way that can be use with 8 bit format ? Thanks
modified on Wednesday, November 3, 2010 10:36 AM
-
I define new bitmap in my code Bitmap image = new Bitmap( width, height, PixelFormat.Format8bppIndexed); ==> that mean that this bitmap is gray scale color. Now, i want to define what color will be at place (i, j) so i usually use ( on 24 color bit ) the method 'SetPixel( i, j, Color.FromRGB( ... ) )' But in this case that the bitmap is on gray scale i cant do it because i have only one value of the color ... and the application crash all the time when i try to call Color.FromRGB(theOneColorIhave) Someone know other way to do set the pixel values - some way that can be use with 8 bit format ? Thanks
modified on Wednesday, November 3, 2010 10:36 AM
You cannot use SetPixel if your bitmap pixel format is indexed, so you will have to use the BitmapData object returned by LockBits[^] method. Edit Ok, this is a little tricky, so I will give a little sample on how to do this before you have to ask again:
unsafe Bitmap ToGrayScale(Bitmap bmp)
{
Bitmap grayBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed);// Prepare the palette ColorPalette pal = grayBmp.Palette; for (int i = 0; i < 256; i++) pal.Entries\[i\] = Color.FromArgb(i, i, i); grayBmp.Palette = pal; // Get BitmapData BitmapData bd = grayBmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); byte\* ptr = (byte\*)bd.Scan0.ToPointer(); for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { Color c = bmp.GetPixel(i, j); // Offset must be Y\*Stride + X\*BytesPerPixel. In this case, // BytesPerPixel is 1 ptr\[j \* bd.Stride + i\] = (byte)((c.R + c.G + c.B) / 3); } } // Unlock grayBmp.UnlockBits(bd); return grayBmp;
}
Sure I should have locked bits also for bmp, becouse GetPixel performance is really bad but, hey, I feel a little lazy today...
modified on Wednesday, November 3, 2010 12:01 PM