public static async Task SaveBitmapToFileAsync(WriteableBitmap image, string fileName = "screenshot")
{
// This stores image to C:\Users\{user}\AppData\Local\Packages\b13e56ac-7531-429d-baf2-003653d989c1_cc4tdm0yr4r3t\LocalState\Screenshots
StorageFolder pictureFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Screenshots", CreationCollisionOption.OpenIfExists);
var file = await pictureFolder.CreateFileAsync(fileName + ".png", CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenStreamForWriteAsync())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream.AsRandomAccessStream());
var pixelStream = image.PixelBuffer.AsStream();
byte[] pixels = new byte[image.PixelBuffer.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)image.PixelWidth, (uint)image.PixelHeight, 96, 96, pixels);
await encoder.FlushAsync();
}
}
}