Over a million developers have joined DZone.

Solving My Problems with Windows Phone

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

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

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)
   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.






Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


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.

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}