IT tutorials
 
Technology
 

Windows Phone 8 : Media and Picture Hubs (part 2) - Accessing Pictures, Storing Pictures

8/10/2013 9:44:31 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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.

Image

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.

 
Others
 
- Windows Phone 8 : Media and Picture Hubs (part 1) - Accessing Music, Playing Music
- Active Directory 2008 : Managing an Enterprise with Groups (part 4) - Developing a Group Management Strategy
- Active Directory 2008 : Managing an Enterprise with Groups (part 3) - Converting Group Scope and Type, Managing Group Membership
- Active Directory 2008 : Managing an Enterprise with Groups (part 2) - Defining Group Naming Conventions, Understanding Group Scope
- Active Directory 2008 : Managing an Enterprise with Groups (part 1) - Understanding the Importance of Groups
- Exchange Server 2010 : Managing Client Access Servers - Deploying Outlook Anywhere
- Exchange Server 2010 : Configuring POP3 and IMAP4 (part 2) - Configuring POP3 and IMAP4 Authentication, Configuring Connection Settings for POP3 and IMAP4, Configuring Message Retrieval Settings for P
- Exchange Server 2010 : Configuring POP3 and IMAP4 (part 1) - Enabling the Exchange POP3 and IMAP4 Services, Configuring POP3 and IMAP4 Bindings
- Microsoft Lync Server 2010 : Planning for Deploying External Services - Sample Scenarios
- Microsoft Lync Server 2010 : Planning for Deploying External Services - Reverse Proxy
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us