c# - 为什么我使用 SetBitmapAsync() 会出现这样的错误?

标签 c# image image-processing win-universal-app

我是 UWP 应用开发的新手,正在尝试构建图像处理应用。我正在使用 https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/imaging 中的代码但是我有这样的异常(exception): SoftwareBitmapSource::SetBitmapAsync 仅支持具有正宽度/高度、bgra8 像素格式和预乘或无 alpha 的 SoftwareBitmap。在代码中 await source.SetBitmapAsync(sbitmap); 我想知道这种方法是否真的有这么多限制,如果有的话,如果有任何替代方法,我应该使用限制最少的方法。代码片段如下

FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileOpenPicker.FileTypeFilter.Add(".jpg");
fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;

var inputFile = await fileOpenPicker.PickSingleFileAsync();
if(inputFile != null)
{
    SoftwareBitmap sbitmap;
    using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
        sbitmap = await decoder.GetSoftwareBitmapAsync();
    }
    var source = new SoftwareBitmapSource();
    await source.SetBitmapAsync(sbitmap);
    **Exception--->**imageControl.Source = source;
}

最佳答案

这是一个已知问题。现在,您可以通过使用 SoftwareBitmap.Convert 获取 Bgra8 预乘 SoftwareBitmap 来显示

SoftwareBitmap displayableImage = SoftwareBitmap.Convert(sbitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

关于c# - 为什么我使用 SetBitmapAsync() 会出现这样的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865978/

相关文章:

iphone - 如何从另一个应用程序打开我的 ios 应用程序中的图像?

java - 在 Android 中将图像缩略图存储到图像中

c# - 强制 asp.net 下拉列表展开

C# 传递一个带有 optional 默认值的整数变量

c# - 当 Microsoft SQL Server 中包含 html 标签时对列进行排序

python - 如何根据图像质量确定使用哪种 OCR 方法

opencv - 查找重复图像的软件链

c# - 使用 SharpSsh 在 ssh 中发送类似 Ctrl+A 的 key

ios - 如何为图像和消息创建条件?

java - 图像 block 的缓冲图像