日期:2014-05-16  浏览次数:20676 次

windows phone:图片(二)

从相机中获取图片

当用户使用手机拍照后,我们可以通过程序获取用户的照片。要完成这个操作需要使用在Microsoft.Phone.Tasks的命名空间中的选择器(Chooser)类和启动器(Launcher)类,启动器不返回任何数据,选择器有数据返回。

让我们来看下面给出的示例:

MainPage.xmal

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Name="image1"/>
</Grid>

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage 

 {

  CameraCaptureTask camera = new CameraCaptureTask();  // 构造函数 

  public MainPage() 

 { 

 InitializeComponent(); 

 camera.Completed += OnCameraCapTureTaskCompleted;//建议在构造函数中绑定事件 

 }

 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)

{

 camera.Show();//显示相机程序

 e.Complete();//表示操作已经完成

 e.Handled = true;//事件完成,停止路由事件转发

 base.OnManipulationStarted(e);

 }

 private void OnCameraCapTureTaskCompleted(object sender, PhotoResult args)

 {

 if (args.TaskResult == TaskResult.OK)//选择器操作完成

 {

 BitmapImage bmp = new BitmapImage();

 bmp.SetSource(args.ChosenPhoto);//将照片数据的流赋给bmp对象

 this.iamge1.Source = bmp;

 }

 }

 }

触摸MainPage页面后会触发手机相机程序,模拟器会模拟一个拍照场景,点击拍照后,选择接受,后程序就会把照片显示在Image元素中