Share via


PixelFormat: why no 8bppGrayscale ??

Question

Wednesday, May 31, 2006 6:27 PM | 1 vote

Hi All,
I am trying to convert 8-bit grayscale TIFF to JPEG using the Bitmap class as intermediary; [.NET Framework 1.1] but there seems to be a puzzling omission in the PixelFormat enumeration that describes the possible types of Bitmap: there is no 8bppGrayscale ! (8 bits per pixel Grayscale).  As is, my grayscale TIFF becomes a Bitmap with the 8bppIndexed PixelFormat by default.  That's no good, because the subsequent call to myBitmap.Save(filenm, ImageFormat.Jpeg) results, not surprisingly, in a 3-band JPEG where the same luminance value is repeated 3 times for each pixel.  Which gets me a grayscale JPEG, but needlessly results in 3 times as much storage in the final repository.  And we are talking about perhaps 500,000 images in the repository.

In my experience (geographical applications) there are 3 commonly used pixel formats: 8bppIndexed (commonly used for digitized maps), 24bppRGB (commonly used for color satellite imagery) and 8bppGrayscale (commonly used for grayscale satellite imagery).  The omission of 8bppGrayscale from the PixelFormat enumeration seems so astounding that I think I must be missing something.  Can anyone tell me what it is ?  (or suggest a workaround?) 
Thanks much.

All replies (4)

Wednesday, May 31, 2006 7:11 PM | 2 votes

To create grayscale bitmap use Format8bppIndexed format with grayscale color palette. You can use the following function to create grayscale palette:

        ColorPalette GetGrayScalePalette()
        {
            Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);

            ColorPalette monoPalette = bmp.Palette;

            Color[] entries = monoPalette.Entries;

            for ( int i = 0; i < 256; i++ )
            {
                entries [ i ] = Color.FromArgb(i, i, i);
            }

            return monoPalette;
        }


Wednesday, May 31, 2006 9:00 PM

Thanks for the response, but I don't think this helps my problem any.  If the Bitmap is 8bppIndexed, then the JPEG made from it will be 3 bands, not one.  I need to make a one-band JPEG from a one-band TIFF.  As it looks now, the Bitmap class is an inadequate intermediary.  The craziest thing is that there is a 16bppGrayScale format, but not 8bppGrayScale, which is probably 100x more common.


Thursday, June 1, 2006 5:50 AM

This is tested solution, but you can continue to look for something else.


Friday, June 2, 2006 8:08 AM

I recommend you to try FreeImage library. They have also a wrapper for C#. http://freeimage.sourceforge.net/