Here's a code snippet how this can be achieved:
private static async TaskSaveWriteableBitmapAsJpeg(WriteableBitmap bmp, string fileName) { // Create file in Pictures library and write jpeg to it var outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { await EncodeWriteableBitmap(bmp, writeStream, BitmapEncoder.JpegEncoderId); } return outputFile; } private static async Task EncodeWriteableBitmap(WriteableBitmap bmp, IRandomAccessStream writeStream, Guid encoderId) { // Copy buffer to pixels byte[] pixels; using (var stream = bmp.PixelBuffer.AsStream()) { pixels = new byte[(uint) stream.Length]; await stream.ReadAsync(pixels, 0, pixels.Length); } // Encode pixels into stream var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint) bmp.PixelWidth, (uint) bmp.PixelHeight, 96, 96, pixels); await encoder.FlushAsync(); }
It's all pretty straightforward actually: The SaveWriteableBitmapAsJpeg creates a file in the user's pictures library and passes the file's stream and an BitmapEncoder ID on to the EncodeWriteableBitmap method which is doing the actual work. EncodeWriteableBitmap first copies the WriteableBitmap pixel buffer into a byte array, then creates the BitmapEncoder based on the file's stream and the desired BitmapEncoder ID, then sets the pixels and flushes all into the stream.
The BitmapEncoder class supports a couple of BitmapEncoder IDs out of the box like JPEG, PNG and more.
No comments:
Post a Comment