Skip to main content
 首页 » 编程设计

c#之在不保存到 UWP C# 文件的情况下在图像控件上显示 SoftwareBitmap

2024年02月25日32lyhabc

有没有办法在不将 SoftwareBitmap 保存到文件的情况下在 Xaml Image 控件上显示 SoftwareBitmap?我想要一种在编辑后快速显示 SoftwareBitmap 的方法。

请您参考如下方法:

I want to have a quick way to display the SoftwareBitmap after edit.

目前,Image 控件仅支持使用 BGRA8 编码和预乘或无 alpha channel 的图像。在尝试显示图像之前,测试以确保它具有正确的格式,如果不正确,请使用 SoftwareBitmap 静态 Convert 方法将图像转换为支持的格式。

if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || 
    softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight) 
{ 
    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); 
} 
 
var source = new SoftwareBitmapSource(); 
await source.SetBitmapAsync(softwareBitmap); 
 
// Set the source of the Image control 
imageControl.Source = source; 

您还可以使用 SoftwareBitmapSource 将 SoftwareBitmap 设置为 ImageBrush 的 ImageSource。更多请引用Create, edit, and save bitmap images .