← 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:
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel contains the name of the application and page title-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - place additional content here-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Button Content="Camera Capture" Height="77" HorizontalAlignment="Left" Margin="27,40,0,0" Name="btnCameraCapture" VerticalAlignment="Top" Width="243" Click="btnCameraCapture_Click" />
- <Image Height="251" HorizontalAlignment="Left" Margin="26,153,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="340" />
- </Grid>
- </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();
}
}
}