Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i define a 8 bit grayscale image directly ?

Tags:

c#

bitmap

The following code is used to create a 8 bit bitmap

Bitmap b = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed);
BitmapData bmd = b.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadWrite, b.PixelFormat);

But when i save it it is saved as a 8 bit color bitmap. Is it possible to directly create a 8 bit grayscale bitmap without creating a 8 bit color bitmap and later having to convert it to a grayscale ?

like image 937
klijo Avatar asked Dec 22 '11 12:12

klijo


People also ask

How do I make an image 8-bit grayscale?

To convert an image from RGB to 8-bit grayscale is a form of downsampling in which 24-bit information must be compressed into an 8-bit range. The simplest method to achieve this is a direct conversion which averages the Red, Green, and Blue values for each pixel.

What is a 8-bit gray scale image?

8 bit color format is one of the most famous image format. It has 256 different shades of colors in it. It is commonly known as Grayscale image. The range of the colors in 8 bit vary from 0-255. Where 0 stands for black, and 255 stands for white, and 127 stands for gray color.

How many grayscale values does an 8-bit image contain?

The number of gray levels, or the dynamic range, of a grayscale image is 2 to the power of the number of bits used to encode the image. For example, in an 8-bit image the number of possible gray values a pixel can have is limited to 256, ranging from pure white to pure black.


1 Answers

I just met this problem and solved it, with the help of this.

But I will put my quick solution here, hope it helps you.
BITMAP files contain four parts, one is the palette, which decides how the colors should be displayed. So here we modify the palette of our Bitmap file:

myBMP = new Bitmap(_xSize, _ySize, _xSize, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, _pImage);
//_pImage is the pointer for our image
//Change palatte of 8bit indexed BITMAP. Make r(i)=g(i)=b(i)
ColorPalette _palette = myBMP.Palette;
Color[] _entries = _palette.Entries;
for (int i = 0; i < 256; i++)
{
    Color b = new Color();
    b = Color.FromArgb((byte)i, (byte)i, (byte)i);
    _entries[i] = b;
}
myBMP.Palette = _palette;
return myBMP;
like image 78
richieqianle Avatar answered Sep 21 '22 06:09

richieqianle