My UWP application "Shanghai Metro" has an option to allow user save the metro graph to local file system. The image file is located under project directory, "Assets\shanghaimetro-xl.jpg", and will ask the user to choose a location when saving the image.
This is a very common functionality, it's also easy to achieve in UWP applications. The most easy way to do it is to use my library "Edi.UWP.Helpers" http://www.nuget.org/packages/Edi.UWP.Helpers/
This library is also open source on GitHub: https://github.com/EdiWang/UWP-Helpers/
In the BitmapExtensions.cs, there's an extension method called:
public static Task<FileUpdateStatus> SaveToPngImage(this WriteableBitmap bitmap, PickerLocationId location, string fileName);
The parameter is type of WriteableBitmap, so the first step is to convert your image file to WriteableBitmap:
public static async Task<WriteableBitmap> LoadWriteableBitmap(string relativePath) { var storageFile = await Package.Current.InstalledLocation.GetFileAsync(relativePath.Replace('/', '\\')); var stream = await storageFile.OpenReadAsync(); var wb = new WriteableBitmap(1, 1); wb.SetSource(stream); return wb; }
Then you can save the image use this method:
var rmbp = await Helper.LoadWriteableBitmap("Assets/shanghaimetro-xl.jpg"); await rmbp.SaveToPngImage(PickerLocationId.PicturesLibrary, "shmetro");
The example code is:
public static async Task<FileUpdateStatus> SaveToPngImage(this WriteableBitmap bitmap, PickerLocationId location, string fileName) { var savePicker = new FileSavePicker { SuggestedStartLocation = location }; savePicker.FileTypeChoices.Add("Png Image", new[] { ".png" }); savePicker.SuggestedFileName = fileName; StorageFile sFile = await savePicker.PickSaveFileAsync(); if (sFile != null) { CachedFileManager.DeferUpdates(sFile); using (var fileStream = await sFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); Stream pixelStream = bitmap.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); } FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(sFile); return status; } return FileUpdateStatus.Failed; }
There's few things to metion:
1. I only wrote "png" format support, you can extend this behaviour yourself.
2. To make sure other thread don't fuck up with your image, use CachedFileManager.DeferUpdates and CachedFileManager.CompleteUpdatesAsync
Comments