← Tutti gli articoli

Camera Photo Capture or Choose in your galleries. Windows Phone 7 Application.

30 April 2011  ·  Windows Phone 7 · Article  ·  776 visite

Windows Phone Tasks provide applications with the ability to integrate with services on the device. In this article I'll show you how use the CameraCaptureTask to allow your application to retrieve a photo or choose one in the WP galleries.

In the MainPage.xaml we insert a button (btnCameraCapture) and an image (image1) control:
  1. <Grid x:Name="LayoutRoot" Background="Transparent">  
  2.        <Grid.RowDefinitions>  
  3.            <RowDefinition Height="Auto"/>  
  4.            <RowDefinition Height="*"/>  
  5.        </Grid.RowDefinitions>  
  6.   
  7.        <!--TitlePanel contains the name of the application and page title-->  
  8.        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  9.            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>  
  10.            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  11.        </StackPanel>  
  12.   
  13.        <!--ContentPanel - place additional content here-->  
  14.        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  15.            <Button Content="Camera Capture" Height="77" HorizontalAlignment="Left" Margin="27,40,0,0" Name="btnCameraCapture"  VerticalAlignment="Top" Width="243" Click="btnCameraCapture_Click" />  
  16.            <Image Height="251" HorizontalAlignment="Left" Margin="26,153,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="340" />  
  17.        </Grid>  
  18.    </Grid>  
The code behid is:

 

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 System.Windows.Media.Imaging;
namespace CameraPhotoTask
{
    public partial class MainPage : PhoneApplicationPage
    {
        CameraCaptureTask ct = new CameraCaptureTask();  //ct is a Camera task
        PhotoChooserTask pct = new PhotoChooserTask();
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            ct.Completed += new EventHandler(ct_Completed);
            pct.Completed += new EventHandler(pct_Completed);
        }
        void pct_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                CameraTaskComplete(e);
            }
        }
        void ct_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                CameraTaskComplete(e);
            }
        }
        private void btnCameraCapture_Click(object sender, RoutedEventArgs e)
        {
            ct.Show();
        }
        private void CameraTaskComplete(PhotoResult e)
        {
            // display the camera photo captured
            var bm = new BitmapImage();
            bm.SetSource(e.ChosenPhoto);
            this.image1.Source = bm;

        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            pct.PixelHeight = 50;
            pct.PixelWidth = 50;
            pct.ShowCamera = true;
            pct.Show();
        }
    }
}
Si è verificato un errore imprevisto. Ricarica

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.