我正在尝试将SoftwareBitmap
转换为CanvasBitmap
(在UWP上)。但是当我使用BitmapPixelFormat.Bgra8
&& BitmapAlphaMode.Premultiplied
时,我得到了一个类似于unsupported pixel format or alpha mode
的错误。
我决定使用以下代码尝试所有可能的格式:
if (softwareBitmap != null)
{
var wasSuccess = false;
foreach (var bitmapAlphaMode in new[] {BitmapAlphaMode.Straight, BitmapAlphaMode.Premultiplied,
BitmapAlphaMode.Ignore})
{
foreach (var bitmapPixelFormat in new[] {
BitmapPixelFormat.Bgra8, BitmapPixelFormat.Gray8, BitmapPixelFormat.Gray16,
BitmapPixelFormat.Yuy2, BitmapPixelFormat.Rgba8, BitmapPixelFormat.Rgba16,
BitmapPixelFormat.Nv12, BitmapPixelFormat.P010, BitmapPixelFormat.Unknown
})
{
if (wasSuccess)
{
break;
}
try
{
SoftwareBitmap.Convert(softwareBitmap, bitmapPixelFormat, bitmapAlphaMode);
var bitmap = CanvasBitmap.CreateFromSoftwareBitmap(
_canvasDevice,
softwareBitmap
);
wasSuccess = bitmap != null;
}
catch (Exception ex)
{
}
}
}
}
但在所有可能的尝试之后,wasSuccess
是false
。(_canvasDevice
已成功初始化,这不是问题所在)。
怎么可能呢?
发布于 2022-04-20 06:50:55
但在所有可能的尝试之后,wasSuccess是错误的。(_canvasDevice已成功初始化,这不是问题所在)。
不是所有的BitmapPixelFormats都支持CanvasBitmap,请参考CreateFromSoftwareBitmap
文档备注部分,您可以发现BitmapPixelFormat.Unknown
BitmapPixelFormat.Gray16
BitmapPixelFormat.Nv12
BitmapPixelFormat.Yuy2
是不支持的。
并不是所有的AlphaModes都可以用于CanvasBitmap
,请参阅此文档。您将发现CanvasBitmap
兼容Pre相乘,忽略alpha模式。只有A8UIntNormalized
A8UIntNormalized
支持Straight
alpha模式。
顺便说一句,在上面的代码段中,SoftwareBitmap.Convert
方法有一个错误。您应该像下面这样将返回值赋值给softwareBitmap,否则它将永远不会更新softwareBitmap属性。
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, bitmapPixelFormat, bitmapAlphaMode);
https://stackoverflow.com/questions/71925510
复制相似问题