Get color depth of a Bitmap

5.1k Views Asked by At

I want to get the bit depth (and color space) of an image file with c#.

Here are my testfiles. Using the properties of the windows explorer, I verified their bit depths (8, 16, 24, 48, 96).

BitmapImage.Format.BitsPerPixel

var source = new BitmapImage(new Uri(path));
int bitsPerPixel = source.Format.BitsPerPixel;

Return 8 for 8-bit grayscale, but 32 for all other types.

Image.PixelFormat

Image img = Image.FromFile(path);
string pixelFormat = img.PixelFormat.ToString();

PixelFormat works for all but the 32-bit floating point image, where it throws a System.OutOfMemoryException.

Image.PropertyItems

This answer suggests the PropertyItems property.

Works like the example above, but throws an exception with 32-bit images.


This answer suggests the use of the Windows API Code Pack. But I would rather use a native c# feature.

ShellObject so = ShellObject.FromParsingName(filename)
int bitdepth = convert.ToInt32(so.Properties.GetProperty(SystemProperties.System.Image.Bitdepth).ValueAsObject);

Is there a built-in method to determine the bit depth of an images, which works with 1, 8, 16, 24, 48, 96 bits?

0

There are 0 best solutions below