[WP7] Add images in the Windows Phone 7 Media Library

The Windows Phone 7 emulator included in the beta developer
tool doesn’t contain
any images in the Media Library. It’s not very easy to test an
application that
asks the user to choose an image to use the application.
This
is
a few lines of code which helps you to add images in the Media Library
emulator. Please note that images are not being saved when you close the
emulator !

When you
look the media library in the WP7, there is nothing:

You must
add in the project the assembly Microsoft.Xna.Framework.Media. It allows you to
access the Media Library and to save images with the SavePicture method :

using
Microsoft.Xna.Framework.Media;

Create
an “Images” folder in the project, and add images (or photos) in this folder.
For each images, change the following properties as the following screenshot:

And use
the following lines of code to add the images in the Media Library:

MediaLibrary myMediaLibrary = new MediaLibrary();
Uri myUri = new Uri(@”Images/Jellyfish.jpg”,
UriKind.Relative);

System.IO.Stream photoStream =
App.GetResourceStream(myUri).Stream;
byte[] buffer = new byte[photoStream.Length];

photoStream.Read(buffer, 0,
Convert.ToInt32(photoStream.Length));

myMediaLibrary.SavePicture(“Jellyfish.jpg”, buffer);

When you have added several images, this is what you can see in the WP7 emulator:

4 thoughts on “[WP7] Add images in the Windows Phone 7 Media Library”

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>