ARTICLE

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

Posted by Dhananjay Kumar Articles | Windows Phone 8 February 08, 2012
In this article we will see how to capture a photo using the camera and saving it in the Media Library.
Reader Level:

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.

Login to add your contents and source code to this article
post comment
     

Very comprehensive

Posted by Jessica Stephen Feb 08, 2012

hi dhananjay, its a very helpful article for me.... thnx for posting....

Posted by Sumita Jain Feb 08, 2012

Good article, Dhananjay. Thanks for sharing.

Posted by Abhi Kumar Feb 08, 2012

Hi Dhananjay. You have presented your article very nicely.

Posted by Stephen Johnson Feb 08, 2012

Thanks for this article ,that is useful.

Posted by Nitin Singh Feb 08, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.