如何检查BitmapImage是否为灰度?

时间:2016-08-28 10:36:47

标签: c# bitmap win-universal-app uwp bitmapimage

我有BitmapImage的{​​{1}}:

StorageFile

我已经能够通过计算using (var stream = await file.OpenAsync(FileAccessMode.Read)) { var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(stream); } PixelWidth属性来检查图像是否为正方形。如何检查图像颜色是否为灰度? This post表示PixelHeightBitmap属性,不幸的是,PixelFormat在UWP中不再可用。

2 个答案:

答案 0 :(得分:2)

您可以使用BitmapDecoder获取BitmapPixelFormat的值。像这样使用它

using (var stream = await file.OpenAsync(FileAccessMode.Read)) {
    var bitmap = new BitmapImage();
    await bitmap.SetSourceAsync(stream);

    var decoder = await BitmapDecoder.CreateAsync(stream);
    BitmapPixelFormat format = decoder.BitmapPixelFormat;

    if(format == BitmapPixelFormat.Gray16 || format == BitmapPixelFormat.Gray8 )
        // Bitmap is grayscale
}

答案 1 :(得分:1)

这取决于您对灰度的定义。

您可能认为灰度是像素格式的问题。在这种情况下,你必须在UWP中找到PixelFormat的替代方案,如果它存在的话(我真的不知道)。

但你也可能有不同的想法。

32位ARGB像素格式的图像能否为灰度?假设您扫描每个像素并且它们都在灰度线上,如下所示:

0x000000
0x010101
0x020202
...
0xfefefe
0xffffff

像素格式允许1600万种组合,但实际上只使用了256种灰色组合。

根据您自己的惯例,您可能会说这仍然是灰度图像,因为它可以转换为一个而不会丢失信息。

如果您正确地阅读了我,您会看到不需要PixelFormat属性存在的问题的答案,但计算起来相当昂贵。