[英]How to convert SVG to WriteableBitmap in UWP C#
我们可以从 UWP 中的 1703 加载 SVG。 谁能提供一种机制,如何将 SVG 转换为 C# UWP 中的 WriteableBitmap。
Unfortunately, there is no such api could covert SVG to WriteableBitmap
image directly, Because when we convert file to WriteableBitmap
we need use BitmapDecoder
class like the following, but BitmapDecoder
does not support svg type.
private static async Task<WriteableBitmap> OpenWriteableBitmapFile(StorageFile file)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
WriteableBitmap image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
image.SetSource(stream);
return image;
}
}
目前我们有一个解决方法,使用RenderTargetBitmap
来呈现您的图像控件并将RenderTargetBitmap
转换为WriteableBitmap
并使用以下方法。
private async void RenderTargetBitmapToWriteableBitmap(UIElement element)
{
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(element);
var pixelBuffer = await bitmap.GetPixelsAsync();
byte[] pixels = pixelBuffer.ToArray();
var wb = new WriteableBitmap((int)bitmap.PixelWidth, (int)bitmap.PixelHeight);
using (Stream stream = wb.PixelBuffer.AsStream())
{
await stream.WriteAsync(pixels, 0, pixels.Length);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.