How to save WriteableBitmap as PNG image with transparency

I crop the circular area from the image for the avatar. I need to get the byte pixel [] of the image and upload it to the server in base64 format. Unfortunately, the SaveJpeg () method does not support transparency pixels outside the selected circle. I tried the ImageTools library, but no platform other than WindowsPhone was able to create a png image from the received byte []. Is there any way to do this?

+1
c # windows-phone save png
source share
2 answers

There is no platform API for this. ToolStack PNG library is an easy solution.

http://toolstack.com/libraries/pngwriter

+2
source share

This code worked for me. Before you try, make sure that your recordable card has a transparent background (you can check by assigning the image source to the image controller). If not, make the background transparent from the controller from which it came.

var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; var file = await localFolder.CreateFileAsync("temp.png", CreationCollisionOption.ReplaceExisting); using (var ras = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) { WriteableBitmap bitmap = imageSource; var stream = bitmap.PixelBuffer.AsStream(); byte[] buffer = new byte[stream.Length]; await stream.ReadAsync(buffer, 0, buffer.Length); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, buffer); await encoder.FlushAsync(); } 

Take a look at this!

+1
source share

All Articles