- 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 System.Windows.Media.Imaging;
- using Microsoft.Phone.Tasks;
- namespace CameraShoot
- {
- public partial class MainPage : PhoneApplicationPage
- {
- //相機捕獲任務執行個體
- CameraCaptureTask cameraCT = new CameraCaptureTask();
- // 構造函數
- public MainPage()
- {
- InitializeComponent();
- //手機拍照功能完成後調用
- cameraCT.Completed += new EventHandler<PhotoResult>(cameraCT_Completed);
- }
- //重寫觸摸屏事件
- protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
- {
- if (e.OriginalSource==txtName)
- {
- //調用相機
- cameraCT.Show();
- }
- //觸摸事件完成
- e.Complete();
- //不在向父元素傳遞
- e.Handled = true;
- base.OnManipulationStarted(e);
- }
- //完成
- void cameraCT_Completed(object sender, PhotoResult e)
- {
- if (e.TaskResult==TaskResult.OK)
- {
- BitmapImage bmp = new BitmapImage();
- //擷取包含檔案流的和Source不同
- bmp.SetSource(e.ChosenPhoto);
- //設定圖檔源
- img.Source = bmp;
- txtName.Text = e.OriginalFileName;
- }
- }
- }
- }