How to check the color depth of a bitmap?

I am working on an application that prints a folder of image files, including JPEG and TIFF. TIFF images are usually black and white (1bpp).

After loading the image, I want to determine if the image has Color or B&W or Grayscale , so I can send the image to the right printer (color printer or black and white printer).

I use the constructor Bitmap image = new Bitmap(filename); to upload an image.

EDIT: Response to checking pixel depth is great for B & W. Any ideas on checking if the image is grayscale without repeating each pixel?

+6
c # image bitmap
source share
3 answers

The correct way to check this is:

For JPEG files, you must check the corresponding properties using the PropertyItems bitmap collection. It may contain appropriate EXIF tags to help determine bit depth. The next step is to analyze the JPEG header and search for the “start frame” marker, and then the number of components in the image.

The last way is to load the JPEG into a Bitmap object and compare the number of pixels with the forum (width * height * bytes_per_pixel). Thus, if you are loading a bitmap, and the number of bytes of actual raw data is equal (width * height), then you know that it is a safe bet that the image has 1 byte per pixel and as such has shades of gray.

The last thing you want to check out is the PixelFormat of the bitmap itself.

For the TIFF format, you must do the same with the PropertyItems collection and check the corresponding tag mentioned in the specification. If they fail, compare the bytes of the image and finally use the PixelFormat property as a last resort.

+3
source share

Just check this property.

 image.PixelFormat 

It will match one of the values ​​in System.Drawing.Imaging.PixelFormat

Although you want to send more than just black and white to your B&W printer, you should also send any gray scales there.

+10
source share

Use the PixelFormat property.

+2
source share

All Articles