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
Then define a global variable,
In constructor of the page, you need to instantiate CameraCaptureTask and attach completed event handler.
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,
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,
In completed event of CameraCaptureTask, make instance of MediaLibrary and call SavePicture method as below,
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.
Follow @debug_mode
Leave a Reply