前置条件:手机和电脑未连接或连接电脑Zune软件关闭(与Zune软件连接时不允许访问图片库); 版本7.1

获取手机图片库图片的两种方式: PhotChooserTask方式和XNA方式进行获取

   PhotChooserTask获取

引用命名空间

//引用
//PhotoChooserTask类用到
using Microsoft.Phone.Tasks;
//BitmapImage类用到
using System.Windows.Media.Imaging;

   隐藏文件代码:

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;
//引用
//PhotoChooserTask类用到
using Microsoft.Phone.Tasks;
//BitmapImage类用到
using System.Windows.Media.Imaging;

namespace ChoosePhoto
{
    public partial class MainPage : PhoneApplicationPage
    {
        //实例化图片选择器
        PhotoChooserTask photoChoose = new PhotoChooserTask();
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            //设置的委托事件
            photoChoose.Completed += new EventHandler<PhotoResult>(photoChoose_Completed);
        }
        //事件处理完得到图片
        void photoChoose_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult==TaskResult.OK)
            {
                //实例化位图
                BitmapImage bi = new BitmapImage();
                //设置位图源
                bi.SetSource(e.ChosenPhoto);
                //设置元素位图
                img.Source = bi;
                txtName.Content = e.OriginalFileName;
                
            }
        }
        /// <summary>
        
/// button事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void txtName_Click(object sender, RoutedEventArgs e)
        {
            //打开图片库
            photoChoose.Show();
            //知识点①
            
//是否显示拍照按钮
            photoChoose.ShowCamera = true;
            //知识点②
            
//设置剪切区域的宽度
            photoChoose.PixelWidth = 50;
            //设置剪切区域的高度
            photoChoose.PixelHeight = 100;
        }
    }
}

相关文章: