DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Operating with image files in a Windows Phone 7 application

Operating with image files in a Windows Phone 7 application

Denzel D. user avatar by
Denzel D.
·
Aug. 15, 10 · Mobile Zone · Interview
Like (0)
Save
Tweet
21.15K Views

Join the DZone community and get the full member experience.

Join For Free

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.

application Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Build Cloud-Native Apps with AWS App Runner, Redis, and AWS CDK
  • Top Soft Skills to Identify a Great Software Engineer
  • Python: A Befitting Approach to Develop AI Web Apps
  • Implementing One and Two Way SSL (Mutual Authentication) for MuleSoft Application

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo