Accessing Pictures From Media Library in Windows Phone


You may come across a requirement to access the entire picture saved in a Media Library of a Windows Phone. The Media Library can be accessed using the MediaLibrary class.

To work with the MediaLibrary class you need to add a reference for Microsoft.Xna.Framework. This class is defined in the namespace Microsoft.Xna.Framework.Media namespace.

To work with the MediaLibrary, you need to make an instance of the MediaLibrary class as below:

MWinPhn1.gif

The Pictures property of the MediaLibrary class returns all the images as a Picture. You can iterate to all images as below:

MWinPhn2.gif

An Image can be fetched as a stream inside a foreach; look below:

MWinPhn3.gif

You can create an Image from a returned stream as below.

MWinPhn4.gif

Eventually you can set imageToShow as source of Image control. For your reference the following code will add all the pictures from Media Library in a listbox called lstImageFromMediaLibrary:

 MediaLibrary m = new MediaLibrary();
            
            foreach (var r in m.Pictures)
            {
                Stream imageStream = r.GetImage();
                 
                var imageToShow = new Image()
                {
                    Source = PictureDecoder.DecodeJpeg(r.GetImage())
                };
 
                lstImageFromMediaLibrary.Items.Add(imageToShow);
            }
        }


You can access the Media Library in the emulator only in debugmode. In this way you can access all the images from Media library. I hope this article is useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all