I am developing a document database application that handles TIFF images. When annotating images, in order for the user-interface to present only black and white as choices for bitonal (Format1bppIndexed) images versus multiple colors for images with a higher color depth I need to test for whether or not a TIFF is bitonal. Here is my function:
private bool IsBitonal(string filePath) { bool isBitonal = false; try { Bitmap bitmap = new Bitmap(sourceFilePath); Graphics graphics = Graphics.FromImage(bitmap); } catch (Exception ex) { isBitonal = true; } return isBitonal; }
It works but it is not graceful. The indexed pixel format exception that is thrown is described here: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx.
Trapping an exception for normal program flow is poor practice. Also, it is conceivable that a different exception could be thrown. Is there a better way to implement an IsBitonal function that does not rely on trapping an exception?
I tried searching the web for information and I found examples of how to load a TIFF image and avoid the exception by converting to a different format but I couldn't find any examples of a simple test for whether or not a TIFF image is bitonal.
Thanks.