我想用 c# 获取图像文件的位深度(和颜色空间)。
这是我的测试文件。使用 Windows 资源管理器的属性,我验证了它们的位深度(8、16、24、48、96)。
BitmapImage.Format.BitsPerPixel
var source = new BitmapImage(new Uri(path));
int bitsPerPixel = source.Format.BitsPerPixel;
对于 8 位灰度返回8 ,但对于所有其他类型返回 32。
图像.像素格式
Image img = Image.FromFile(path);
string pixelFormat = img.PixelFormat.ToString();
PixelFormat适用于除 32 位浮点图像之外的所有图像,它会抛出System.OutOfMemoryException
.
Image.PropertyItems
这个答案建议使用 PropertyItems 属性。
与上面的示例类似,但会引发 32 位图像异常。
这个答案建议使用 Windows API 代码包。但我宁愿使用原生 c# 功能。
ShellObject so = ShellObject.FromParsingName(filename)
int bitdepth = convert.ToInt32(so.Properties.GetProperty(SystemProperties.System.Image.Bitdepth).ValueAsObject);
是否有内置方法来确定图像的位深度,适用于 1、8、16、24、48、96 位?