How to handle high resolution photography in Windows Phone 8.1

I am trying to load images from FileOpenPicker and save it to WritableBitmap so I can work with it later. It works, but when I upload a high resolution image (for example jpg 2592x3888) my app crashes. And it takes too long to process large images. So my question is, what am I doing wrong here? What's the best approach?

StorageFile file = args.Files[0];
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
    App.context.Image = new WriteableBitmap(1, 1);
    int width = (int)Frame.ActualWidth;
    int height = (int)Frame.ActualHeight;
    App.context.Image = await BitmapFactory.New(1, 1).FromStream(stream);
    App.context.Image = App.context.Image.Resize(width, height, WriteableBitmapExtensions.Interpolation.NearestNeighbor);


PS: This is really a working version - I will not use this width and height.


source to share

1 answer

You can use below code to decode image for lower resolution.

using (var storageStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, storageStream);
var pixelStream = yourWriteableBitmap.PixelBuffer.AsStream();
var pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);

encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)yourWriteableBitmap.PixelWidth, (uint)yourWriteableBitmap.PixelHeight, 48, 48, pixels);
await encoder.FlushAsync();


you can refer to this question for more info: WriteableBitmap SaveJpeg is missing for generic apps



All Articles