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.
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
Declare variables.
ICollection<MediaSource> mediaSources; MediaLibrary mediaLib; PictureCollection picCollection; Texture2D pic;
Call MediaSource.GetAvailableMediaSources to retrieve the available media on the device.
// Retrieve all available media libraries on the device mediaSources = MediaSource.GetAvailableMediaSources();
Call MediaLibrary to get the media libraries from the device.
mediaLib = new MediaLibrary();
Obtain the picture collection from the media library.
picCollection = mediaLib.Pictures;
Get a picture's texture from the collection.
pic = Texture2D.FromStream(GraphicsDevice, picCollection[currentPic].GetImage());
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
- Playing a Video
Demonstrates how to use the VideoPlayer to play a video. - Playing a Song from a URI
Demonstrates how to use the MediaPlayer to play a song from a Uniform Resource Identifier (URI). - Displaying Pictures and Playing Video
Provides overviews about how to use the Media API to retrieve system media, including pictures, songs, and video.
Reference
- MediaLibrary Class
Provides access to songs, playlists, and pictures in the device's media library. - PictureCollection Class
A collection of pictures in the media library.