本文介绍了将 Win2D CanvasRenderTarget 转换为 BitmapImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个函数,它接受我的 ICanvasEffect 作为参数(在我的例子中是一个 Win2D BlendEffect),我想转换 ICanvasEffectstrong>CanvasRenderTarget 到 BitmapImage 以便我可以在 UWP Image 控件中使用它:
I'm trying to write a function which accepts my ICanvasEffect as a parameter (which in my case is a Win2D BlendEffect), and I want to convert the CanvasRenderTarget to a BitmapImage so that I can use it in a UWP Image control:
private async Task<BitmapImage> GetBitmapImage(CancellationToken ct, ICanvasImage effect)
{
using (var target = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), 320f, 240f, 96))
{
using (var ds = target.CreateDrawingSession())
{
// Draw the image with the supplied ICanvasImage
ds.DrawImage(effect);
}
//await target.SaveAsync(outputStream, CanvasBitmapFileFormat.Jpeg).AsTask(ct);
}
}
正如您在注释代码中看到的那样,CanvasRenderTarget 有一个 SaveAsync 方法,我可以使用它将其保存到流中,但如何保存?
As you see in the commented code, CanvasRenderTarget has a SaveAsync method I can use to save it to a Stream, but how?
推荐答案
想通了:
using (var stream = new InMemoryRandomAccessStream())
{
await target.SaveAsync(stream, CanvasBitmapFileFormat.Jpeg).AsTask(ct);
var bmp = new BitmapImage();
stream.Seek(0);
await bmp.SetSourceAsync(stream);
}
这篇关于将 Win2D CanvasRenderTarget 转换为 BitmapImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!