How to open and work with large photos on Windows Phone

How to open and work with large photos on Windows Phone

Usually when you try to open a large photo on Windows Phone, it gets resized to a smaller one. What is a “large”, and what is a “smaller” photo? At the moment of writing this article, every photo larger than 2000 x 2000 pixels is considered large and gets resized to smaller version (longer side is then 2000 pixels), which can be a problem if you’re writing a photo editing software. I solved this problem in my free photo editing app called Photo Light, and in this article I’ll show you how to do it in your app. Have in mind that if you use images that are larger than 2000 x 2000 pixels, they will be significantly slower to appear and the memory consumption can become a problem!

If you read about the performance considerations in a WP7 app, you’ll notice that the recommended way of loading large images is by loading the image into a T:System.Windows.Media.Imaging.WriteableBitmap and using the LoadJpeg(WriteableBitmap, Stream) extension method. The problem here is that when your write photo editing software, you need to be ready to open photos of all sorts of dimensions, and not only hardcoded ones. In order to create the WriteableBitmap, you need to use the photo height and width in the constructor, which can be a problem if you don’t know the size in advance. So, the obvious solution is to somehow get the size of the photo before creating the WriteableBitmap. How to do it?

Get photo size before creating WriteableBitmap

In order to get the photo size, you need to use some sort of an exif library which can read that information from the stream you get. The best one I’ve found so far is the modified version that you can find in this project WindowsPhoneApplication63.zip, created by Tim Heuer for this article. He modified the library published on CodeProject, and I wrote about it earlier in this article. You need to grab the dll library called ExifLib and add it to the references of your project which needs to work with large photos. How does the library work?

Create a new PhotoChooserTask that you’ll use for grabbing photos from the libary. When it returns to your app, it returns a PhotoResult object. You also need to create the JpegInfo object which is the vital part of ExifLib library.

PhotoChooserTask photoChooser = new PhotoChooserTask();  
ExifLib.JpegInfo info;  

Then when the PhotoResult object is returned, you create the JpegInfo object:

void PhotoChosenTask_Completed(object sender, PhotoResult e)  
{
    if (e.TaskResult == TaskResult.OK)
    {
        info = ExifLib.ExifReader.ReadJpeg(e.ChosenPhoto, e.OriginalFileName);
    }
}

As you can see, all you need to pass to the constructor is the chosen photo and the original file name.

Now, the info object holds the Width and Height property of the photo that you need for creating the WriteableBitmap:

int wid = info.Width;  
int hei = info.Height;  
WriteableBitmap bmp = new WriteableBitmap(wid, hei);  
bmp.LoadJpeg(e.ChosenPhoto);  

And there you have it – the WriteableBitmap object called bmp now holds your large photo!

screenshots 634582672034280000

This is how my app Photo Light opens the photo and, as you can see, it shows that the size is 2560x1920 pixels, which is the resolution of a 5MP photo (camera minimum requirement on a Windows Phone device).

Again, have in mind that if you use images that are larger than 2000 x 2000 pixels, they will be significantly slower to appear and the memory consumption can become a problem!

If you still haven’t, download Photo Light – it’s free and it does the job! Leave a comment or find me on Twitter

Igor Ralic

igor ralic

View Comments
Microsoft Certified Solutions Developer: Windows Store Apps in C#