Accessing Pictures from a Picture Album

This section demonstrates how to access pictures in a picture album by using the Media API on Windows Phone.

Complete Sample

The code in the topic shows you the technique for using Media API to access pictures. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download ShowPictures.zip

Accessing Pictures

This example retrieves and displays pictures from a device's media library.

Note

Picture collections will always be empty on Xbox 360 and Windows; therefore, the following example work only on Windows Phone.

To retrieve pictures

  1. Declare variables.

    ICollection<MediaSource> mediaSources;
    MediaLibrary mediaLib;
    PictureCollection picCollection;        
    Texture2D pic;
    
  2. Call MediaSource.GetAvailableMediaSources to retrieve the available media on the device.

    // Retrieve all available media libraries on the device
    mediaSources = MediaSource.GetAvailableMediaSources();
    
  3. Call MediaLibrary to get the media libraries from the device.

    mediaLib = new MediaLibrary();
    
  4. Obtain the picture collection from the media library.

    picCollection = mediaLib.Pictures;
    
  5. Get a picture's texture from the collection.

    pic = Texture2D.FromStream(GraphicsDevice, picCollection[currentPic].GetImage());
    
  6. Display the picture on-screen.

    protected override void Draw(GameTime gameTime)
    {           
        GraphicsDevice.Clear(Color.CornflowerBlue);
        Vector2 pos = Vector2.Zero;
        spriteBatch.Begin();
        spriteBatch.Draw(pic, pos, Color.White);
        spriteBatch.End();
    
        base.Draw(gameTime);
    }
    

Concepts

Reference