标签: bitmapencoder

UWP将图像编码为PNG

我通过URI(Web或文件系统)获取图像,并希望将其编码为PNG并保存到临时文件:

var bin = new MemoryStream(raw).AsRandomAccessStream();  //raw is byte[]
var dec = await BitmapDecoder.CreateAsync(bin);
var pix = (await dec.GetPixelDataAsync()).DetachPixelData();

var res = new FileStream(Path.Combine(ApplicationData.Current.LocalFolder.Path, "tmp.png"), FileMode.Create);
var enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, res.AsRandomAccessStream());
enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, dec.PixelWidth, dec.PixelHeight, 96, 96, pix);
await enc.FlushAsync();  //hangs here
res.Dispose();
Run Code Online (Sandbox Code Playgroud)

问题是,这段代码挂起了await enc.FlushAsync().请帮忙!谢谢.

c# bitmapimage bitmapencoder uwp

6
推荐指数
1
解决办法
2038
查看次数

标签 统计

bitmapencoder ×1

bitmapimage ×1

c# ×1

uwp ×1