Reader Level:
Article
Microsoft Phone

Capture Picture From Camera and Save in Media Library in Windows Phone

By Dhananjay Kumar on Feb 08, 2012
In this article we will see how to capture a photo using the camera and saving it in the Media Library.

In this article we will see how to capture a photo using the camera and saving it in the Media Library.

The CameraCaptureTask chooser is used to capture a photo using the Windows Phone's Camera. To work with the CameraCaptureTask, first you need to add the namespace:

CptrWinPhn1.gif

Then define a global variable:

CptrWinPhn2.gif

In the constructor of the page, you need to instantiate a CameraCaptureTask and attach a completed event handler.

CptrWinPhn3.gif

Next you need to show the camera to the user. You can call the Show function anywhere as per your business requirement however I am calling it on the click event of a button as below:

CptrWinPhn4.gif

Now in the completed event of the CameraCaptureTask we need to save the image in the Media Library. To work with the Medialibrary, you need to add a reference of Microsoft.Xna.Framework. After adding the reference, add the following namespace:

CptrWinPhn5.gif

In the completed event of CameraCaptureTask, make an instance of the MediaLibrary and call the SavePicture method as below:

CptrWinPhn6.gif

As you see, the SavePicture function takes two input parameters. It takes the Name of the picture as one input parameter and a picture stream as another. In this example we are saving the picture taken from the camera. You may also save a picture downloaded from services as a stream.

For your reference, the full source code is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
 
namespace PhoneApp17
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        CameraCaptureTask cameraTask; 
        public MainPage()
        {
            InitializeComponent();
            cameraTask = new CameraCaptureTask();
            cameraTask.Completed += new EventHandler<PhotoResult>(cameraTask_Completed);
            
        }
 
        void cameraTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MediaLibrary medialibrary = new MediaLibrary();
                medialibrary.SavePicture("givenameofimage", e.ChosenPhoto);
               
            }
        }
 
        private void btnShowCamera_Click(object sender, RoutedEventArgs e)
        {
            cameraTask.Show();         
        }
    }
}


In this way you can save a picture to the Media Library. I hope this article is useful. Thanks for reading.