Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

UWPでWin2Dで描いた画像を保存しよう

$
0
0

こんな感じでいけました。

CanvasがCanvasControlの変数になります。

var picker = new FileSavePicker();
picker.FileTypeChoices.Add("png", new List<string> { ".png" });
var file = await picker.PickSaveFileAsync();
if (file == null) { return; }

var size = this.Canvas.RenderSize;
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(this.Canvas, (int)size.Width, (int)size.Height);

var displayInformation = DisplayInformation.GetForCurrentView();

using (var s = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, s);
    encoder.SetPixelData(
        BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Ignore,
        (uint)renderTargetBitmap.PixelWidth,
        (uint)renderTargetBitmap.PixelHeight,
        displayInformation.LogicalDpi,
        displayInformation.LogicalDpi,
        (await renderTargetBitmap.GetPixelsAsync()).ToArray());
    await encoder.FlushAsync();
}

参考

stackoverflow.com


Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>