How can I check the color depth of a Bitmap?

Solution 1:

Just check this property

image.PixelFormat

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

Though you would want to send more than just Black & White to the B&W printer, you should also send any gray scales there as well.

Solution 2:

The proper way to check this is:

For JPEG files you should check the appropriate properties using the PropertyItems collection of the Bitmap. This may contain the appropriate EXIF tags to help determine the bit depth. The next step would be to parse the JPEG header and look for the 'start of frame' marker and then the number of components in the image.

The final method is to load the JPEG into a Bitmap object and compare the number of pixels with the forumla (width * height * bytes_per_pixel). So if you load the bitmap and number of bytes of actual raw data is equal to (width * height) then you know it's a safe bet that the image has 1 byte per pixel and as such is grayscale.

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

For the TIFF file format you should do the same thing using the PropertyItems collection and check the appropriate tag mentioned in the spec. If these fail then do the image byte comparison and finally use the PixelFormat property as a last resort.