Over a million developers have joined DZone.

Using Windows Phone Camera on Image Control

· Mobile Zone

This will be a very small article on how to capture images using Windows Phone Camera and Display on Image Control, and one of the issue I encountered while using camera in Windows Phone.

Let's write the code:

Step 1: Add an image button inside ContentPanel of MainPage.xaml.

<Image Height="450" HorizontalAlignment="Left" Margin="50,0,0,0" Name="Image1" VerticalAlignment="Top" Width="400" />

Step 2: Add a button in the application bar or inside ContentPanel.

<phone:PhoneApplicationPage.ApplicationBar>
   <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
      <shell:ApplicationBarIconButton Text="camera" IconUri="appbar.camera.png" x:Name="btnCamera" Click="btnCamera_Click"/>
   </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

Step 3: Add a Using Directive for Microsoft.Phone.Tasks and System.Windows.Media.Imaging.

using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;

Step 4: Add a class level variable of CameraCaptureTask in MainPage.xaml.cs

public partial class camera : PhoneApplicationPage
{
   CameraCaptureTask cameraCapture = new CameraCaptureTask();

Step 5: Add a camera capture completed event in the constructor of MainPage.xaml.

cameraCapture.Completed += new EventHandler<PhotoResult>(cameraCapture_Completed);

Step 6: Add cameraCapture_Completed method in MainPage.xaml.cs which will be executed once the photo is captured.

void cameraCapture_Completed(object sender, PhotoResult e)
{
   if (e.TaskResult == TaskResult.OK)
   {
      Image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
   }
}

Step 7: Add btnCamera_Click method which will show the camera to take picture.

private void btnCamera_Click(object sender, EventArgs e)
{
   try
   {
      cameraCapture.Show();
   }
   catch (Exception ex)
   {
   }
}

Step 8: Connect your device to your PC and wait for Zune to start and sync. Then, deploy the app and click on the button. You will notice it won't work.

Now, disconnect your device from the USB and run the application again. Click on the button to turn on the camera and take photo. Once you click on the camera button, there will be two buttons: accept, and retake.

Windows Phone Camera

Once you click on the accept button, the taken photo will appear on the image control of MainPage.xaml.

Windows Phone CameraCaptureTask

Important: Device should not be connected to PC when you are capturing photo using CameraCaptureTask.

This ends the article about taking photos using camera and display on image control.

 

 

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}