Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Operating with image files in a Windows Phone 7 application

DZone's Guide to

Operating with image files in a Windows Phone 7 application

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Images are one thing that is quite interesting to work with in a Windows Phone 7 application. In fact, for some specific applications manipulating image files can be a necessity (for example, when the application saves and loads edited photos).

As you probably already know, the files that are created by a Windows Phone 7 application are stored inside the isolated storage – a storage unit designated for the current application that no other application can access.

Unlike regular text files that can be read as plain string units, images are handled in a different way. Even if you used Silverlight before, opening image files in a Windows Phone 7 application will require you to use a different approach, since you won’t be directly in control of the storage space.

Basically, saving an image locally (for example, from a web site) is quite easy. If you’ve read my previous article on file handling , then I can say that the same principles are applied here.

To show an example, I am going to use a WebClient to download an image and store it locally.

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

I am referencing the OpenReadCompleted event handler that will be triggered when the data from the specified resource will be completely cached. Therefore, all I will have to do is store it for permanent access. Here is how the actual event handler looks like:

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var resInfo = new StreamResourceInfo(e.Result, null);
var reader = new StreamReader(resInfo.Stream);

byte[] contents;
using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
{
contents = bReader.ReadBytes((int)reader.BaseStream.Length);
}

IsolatedStorageFileStream stream = file.CreateFile("file.jpg");
stream.Write(contents, 0, contents.Length);
stream.Close();
}

This way, I am converting the downloaded stream to a byte array and I am storing it in the root folder as a JPEG image. The actual image download is triggered via OpenReadAsync:

client.OpenReadAsync(new Uri("http://www.google.com/images/srpr/nav_logo14.png"));

Here I am passing a sample image URL (feel free to change it to whatever image you want). So it is downloaded and stored. Now it’s time to read the contents.

To test this, I inserted an Image control on the page that will display the stored image. To retrieve the image, an instance of IsolatedStorageFileStream is used:

IsolatedStorageFileStream stream = new IsolatedStorageFileStream("filet.jpg", FileMode.Open, file);

var image = new BitmapImage();
image.SetSource(stream);

image1.Source = image;

Although the first thing that comes to mind would be using a relative URI for the image source, it will not work on a Windows Phone 7 application because the user has no direct control over the storage unit.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}