private async void SaveImageFile(WriteableBitmap finalImage) { // Set up and launch the Save Picker FileSavePicker fileSavePicker = new FileSavePicker(); fileSavePicker.FileTypeChoices.Add("PNG", new string[] { ".png" }); StorageFile savefile = await fileSavePicker.PickSaveFileAsync(); if (savefile == null) { return; } IRandomAccessStream stream = await savefile.OpenAsync(FileAccessMode.ReadWrite); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); // Get pixels of the WriteableBitmap object Stream pixelStream = finalImage.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); // Save the image file with png extension encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)finalImage.PixelWidth, (uint)finalImage.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); ShowFlyoutAboveInkToolbar?.Invoke("Image Saved"); }
private async void SaveStrokes(StorageFolder storageFolder, List <InkStrokeContainer> _strokes) { // Remove existing files from storageFolder if (storageFolder != null) { IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); foreach (var f in files) { if (f.FileType.Equals(".gif") && f.DisplayName.StartsWith("InkStroke")) { await f.DeleteAsync(); } } } // Get all strokes on the InkCanvas. IReadOnlyList <InkStroke> currentStrokes; int i = 0; foreach (var item in _strokes) { if (item.GetStrokes().Count > 0) { // Strokes present on ink canvas. currentStrokes = item.GetStrokes(); var file = await storageFolder.CreateFileAsync("InkStroke" + i.ToString() + ".gif", CreationCollisionOption.ReplaceExisting); // When chosen, picker returns a reference to the selected file. if (file != null) { // Prevent updates to the file until updates are finalized with call to CompleteUpdatesAsync. CachedFileManager.DeferUpdates(file); // Open a file stream for writing. IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite); // Write the ink strokes to the output stream. using (IOutputStream outputStream = stream.GetOutputStreamAt(0)) { await item.SaveAsync(outputStream); await outputStream.FlushAsync(); } stream.Dispose(); // Finalize write so other apps can update file. Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); } i++; } } ShowFlyoutAboveInkToolbar?.Invoke("Project Saved"); }