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

In this post we will see the way to capture a photo using camera and saving that in Media Library.

CameraCaptureTask chooser is used to capture photo using Windows Phone Camera. To work with CameraCaptureTask , first you need add namespace

image

Then define a global variable,

image

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

image

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

image

 

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

image

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

image

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

For your reference full source code is given as 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 picture to Media Library. I hope this post is useful. Thanks for reading.

 

4 responses to “Capture Picture from Camera and Save in Media Library in Windows Phone”

  1. […] Capture Picture from Camera and Save in Media Library in Windows Phone (Dhananjay Kumar) […]

  2. […] Capture Picture from Camera and Save in Media Library in Windows Phone […]

  3. […] Capture Picture from Camera and Save in Media Library in Windows Phone […]

  4. […] Capture Picture from Camera and Save in Media Library in Windows Phone – Dhananjay Kumar […]

Leave a comment

Create a website or blog at WordPress.com