3. Accessing Pictures
In addition to accessing the music on the phone, the MediaLibrary
class also exposes the pictures that are stored on the phone. The MediaLibrary
class has the following three properties that give you access to the pictures on the phone:
• Pictures: This is a collection of all the pictures on the phone.
• SavedPictures: This is the special folder for saved pictures on the phone.
• RootPictureAlbum: This is the starting point for a hierarchical collection of pictures on the phone.
The Pictures
and SavedPictures
properties are simple IEnumerable
collections of Picture
objects. For example, to access the name of each picture you could iterate through the list of Picture
objects, like so:
using (var library = new MediaLibrary())
{
foreach (Picture thePicture in library.Pictures)
{
theListBox.Items.Add(thePicture.Name);
}
}
The Picture
class exposes the name,
date, height, and width of the picture as well as to which album the
picture belongs. To get at the actual picture, you can call either GetThumbnailImage
or GetImage
to get a stream of the picture. GetThumbnailImage
retrieves a much smaller version of the picture, whereas GetImage
retrieves the full-size picture. You can wrap the image you retrieve with a BitmapImage
object in the imaging system (in the System.Windows.Imaging
namespace) to be able to show it as the source of an Image
element, like so:
using (var library = new MediaLibrary())
{
foreach (Picture thePicture in library.Pictures)
{
// Get the Picture Stream
Stream imageStream = thePicture.GetImage();
// Wrap it with a BitmapImage object
var bitmap = new BitmapImage();
bitmap.SetSource(imageStream);
// Create an Image element and set the bitmap
var image = new Image();
image.Source = bitmap;
// Add it to the ListBox to show it.
theListBox.Items.Add(image);
}
}
Although accessing individual pictures is useful, the MediaLibrary
class also gives you access to the picture album structure. The PictureAlbum
class contains a collection of that album’s pictures (called Pictures
) as well as a collection of the albums in that album (called Albums
). The MediaLibrary.RootPictureAlbum
is the top-level album and will contain a hierarchical collection of
all albums and pictures. For instance, you could iterate through all the
albums using a recursive function, like so:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
using (var library = new MediaLibrary())
{
AddAlbum(library.RootPictureAlbum, "");
}
}
void AddAlbum(PictureAlbum theAlbum, string indention)
{
// Show Album Name
theListBox.Items.Add(string.Concat(indention,
"Album: ",
theAlbum.Name));
// List Albums in this Album
foreach (PictureAlbum subAlbum in theAlbum.Albums)
{
AddAlbum(subAlbum, string.Concat(indention, " "));
}
// List Pictures
foreach (Picture thePicture in theAlbum.Pictures)
{
theListBox.Items.Add(string.Concat(indention,
" - ",
thePicture.Name));
}
}
Walking through the list of albums results in a list of the albums and their resultant pictures, as shown in Figure 2.
FIGURE 2 Displaying the albums and pictures
4. Storing Pictures
The MediaLibrary
class also
enables you to save pictures to the phone. The only limitation is that
you can save images only directly to a special album called “Saved
Pictures.” You save the picture to this album using the MediaLibrary
.SavePicture
method. This method takes the name of the picture file as well as the
contents of the picture (usually a stream or an array of bytes). For
example, to capture a photo using the CameraCaptureTask
, you could take the stream from the Completed
event, like so:
public partial class MainPage : PhoneApplicationPage
{
CameraCaptureTask takePicture = new CameraCaptureTask();
// Constructor
public MainPage()
{
InitializeComponent();
MouseLeftButtonUp += new
MouseButtonEventHandler(MainPage_MouseLeftButtonUp);
// Handle the picture after it's been taken
takePicture.Completed += new
EventHandler<PhotoResult>(takePicture_Completed);
}
void MainPage_MouseLeftButtonUp(object sender,
MouseButtonEventArgs e)
{
// Take the picture
takePicture.Show();
}
void takePicture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
// Use the Media Library to save the picture
using (MediaLibrary theLibrary = new MediaLibrary())
{
// The name supplied will be suffixed with .jpg
theLibrary.SavePicture("My Camera Photo", e.ChosenPhoto);
}
}
}
}
The MediaLibrary
class’s SavePicture
method takes a name (that it suffixes with “.jpg”) and the photo
itself, which can be in the form of a stream (like this example shows)
or a byte array. After you save the picture, it shows up in a new album
called “Saved Pictures” and is accessible from the MediaLibrary
class’s SavedPictures
property.