4

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?

Community
  • 1
  • 1
Leander
  • 508
  • 6
  • 21
  • It throws `OutOfMemoryException` because 32-bit floating point bitmaps aren't supported by GDI+ (sadly, "out of memory" is the general error number GDI gives). If you need to support them, you'll need a 3rd party library. – Luaan Feb 02 '17 at 15:40
  • @Luaan Does this mean 32-bit are not supported entirely? Because they can be converted to a Bitmap, see my first question: http://stackoverflow.com/a/42000617/6879283 – Leander Feb 02 '17 at 15:42
  • 1
    That's not conversion to `Bitmap`, rather to `BitmapSource`. That's something entirely different. – Luaan Feb 02 '17 at 15:44
  • Converted/decoded but not kept in 32bit flp.. Your test file link is broken btw – TaW Feb 02 '17 at 15:44
  • @Luaan I thought I could convert that to a Bitmap object. http://stackoverflow.com/a/2897325/6879283 I also understand that I loose the information. I would rather determine the bit depth and then import the 32-bit image differently, rather than catching the exception, because it could be caused by actually running out of memory. Even if I just wanted to display an error: "32-bit Not supported" I must find out if the file is a 32-bit image, right? – Leander Feb 02 '17 at 15:47
  • 3
    This is not in general possible with the System.Drawing namespace. The codecs included with .NET were designed to create in-memory bitmaps that are easily and quickly processed and displayed, they will convert from the pixel format in the file if necessary to achieve that goal. You'll need a library to do this, pretty tough shopping if you want-em-all. Lead Tools is the thousand pound gorilla. – Hans Passant Feb 02 '17 at 15:48
  • There's a lot of guessing involved when parsing the image file. How do you tell a difference between a header-less bitmap that's too big to fit in memory and a file that isn't a bitmap at all? I've seen far more of those in the wild than FP bitmaps. `Bitmap` doesn't know FP bitmaps and that's that. If you want to support them in any way, you need to use a library that does. And again, `Bitmap` is a GDI object, `BitmapSource` isn't - just because you can convert non-GDI data to a GDI bitmap doesn't mean GDI supports that data. You explicitly ask `BitmapSource` to convert to Argb32. – Luaan Feb 02 '17 at 15:54

0 Answers0