Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Solving My Problems with Windows Phone

DZone's Guide to

Solving My Problems with Windows Phone

· Web Dev Zone ·
Free Resource

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

This is going to be a very small article on to capture images using camera and display on image control, as well as one of the issues I encountered while using camera in Windows Phone.

Let's write code!

Step 1: Add a 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 using directive of 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 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: Now connect your device to PC and wait for Zune to start and sync.  Now deploy the app and click on the button; you will notice it won't work.

Now disconnect your device from USB and run the application again and 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 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 of taking photo using camera and display on image control.

 

 


 

 

 

Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}