windows phone 获取手机图片库中图片(4)
前置条件:手机和电脑未连接或连接电脑Zune软件关闭(与Zune软件连接时不允许访问图片库); 版本7.1
获取手机图片库图片的两种方式: PhotChooserTask方式和XNA方式进行获取
PhotChooserTask获取
引用命名空间
//引用
//PhotoChooserTask类用到
using Microsoft.Phone.Tasks;
//BitmapImage类用到
using System.Windows.Media.Imaging;
//PhotoChooserTask类用到
using Microsoft.Phone.Tasks;
//BitmapImage类用到
using System.Windows.Media.Imaging;
隐藏文件代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
}
}
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;
}
}
}
知识点①:PhotoChooserTask类的属性,该属性表示是否显示启动相机按钮,一般显示比较好,增加图片来源途径
知识点②:通俗的讲应该是设置或者获取截取区域的高度和宽度
小结: 有裁剪功能是一大特色
XNA方式获取
引用命名空间
//引用XNA库
using Microsoft.Xna.Framework.Media;
//BitmapImage类用到
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;
//BitmapImage类用到
using System.Windows.Media.Imaging;
MediaLibrary应该可以译为媒体库,它包含了影音,图片,是所有媒体的集合
隐藏文件代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
//引用XNA库
using Microsoft.Xna.Framework.Media;
//BitmapImage类用到
using System.Windows.Media.Imaging;
namespace XNAChoosePhoto
{
public partial class MainPage : PhoneApplicationPage
{
//知识点①
MediaLibrary mediaL = new MediaLibrary();
// 构造函数
public MainPage()
{
InitializeComponent();
//获得图片的方法
GetPic();
}
/// <summary>
/// 获得一张图片
/// </summary>
void GetPic()
{
//获得媒体库中所有的图片
PictureCollection pic = mediaL.Pictures;
if (pic.Count>1)
{
BitmapImage bi = new BitmapImage();
//知识点②
Picture pc=pic[0];
bi.SetSource(pc.GetImage());
img.Source = bi;
txtName.Text = "图片名称:" + pc.Name + ";\n该图片相集名称:" + pc.Album.Name + ";\n获取照片摄制时间" + pc.Date;
}
}
}
}
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;
//引用XNA库
using Microsoft.Xna.Framework.Media;
//BitmapImage类用到
using System.Windows.Media.Imaging;
namespace XNAChoosePhoto
{
public partial class MainPage : PhoneApplicationPage
{
//知识点①
MediaLibrary mediaL = new MediaLibrary();
// 构造函数
public MainPage()
{
InitializeComponent();
//获得图片的方法
GetPic();
}
/// <summary>
/// 获得一张图片
/// </summary>
void GetPic()
{
//获得媒体库中所有的图片
PictureCollection pic = mediaL.Pictures;
if (pic.Count>1)
{
BitmapImage bi = new BitmapImage();
//知识点②
Picture pc=pic[0];
bi.SetSource(pc.GetImage());
img.Source = bi;
txtName.Text = "图片名称:" + pc.Name + ";\n该图片相集名称:" + pc.Album.Name + ";\n获取照片摄制时间" + pc.Date;
}
}
}
}
知识点①:MediaLibrary是个很有用的类,比如你可以获得里面的歌曲
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
MediaLibrary mediaL = new MediaLibrary();
SongCollection sc = mediaL.Songs;
if (sc.Count > 1)
{
Song s = sc[0];
MediaPlayer.Play(s);
}
SongCollection sc = mediaL.Songs;
if (sc.Count > 1)
{
Song s = sc[0];
MediaPlayer.Play(s);
}
知识点②:Picture类的一些属性或者方法会用得到,比如当前图片所在的相集,相集下是否还会包含相集,以及相集的名称,时间等
小结:整体的思路是从集合中逐级剥离,媒体库(影音,图片)-------》图片集合------》图片-----》图片相关属性;之前忘记从哪个博主那里看到如果用到XNA中的类,在提交应用的时候不能通过
运行后的效果图: