原文:与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成

[索引页]
[源码下载]

与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1) 之媒体

  • 通过 WriteableBitmap 编辑图片,以及保存图片到相册
  • 与图片的上下文菜单“应用程序...”关联
  • 与图片的上下文菜单“共享...”关联
  • 与 Windows Phone 的图片中心集成

示例
1、演示如何通过 WriteableBitmap 编辑图片,以及保存图片到相册
WriteableBitmapDemo.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.WriteableBitmapDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <Image x:Name="img" /> <Button x:Name="btnSaveToCameraRollAlbum" Content="保存到图片 hub 的“本机拍照”" Click="btnSaveToCameraRollAlbum_Click" /> <Button x:Name="btnSaveToPictureAlbum" Content="保存到图片 hub 的“相册”" Click="btnSaveToPictureAlbum_Click" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>

WriteableBitmapDemo.xaml.cs

/*
* 本例演示编解码 jpeg 格式图片,通过 WriteableBitmap 修改图片,以及如何保存图片到图片中心的“本机拍照”和“相册”
*
* Picture - 媒体库中的图片对象
* MediaLibrary - 媒体库,用于访问设备中的图片、音乐、播放列表等
* SavePicture(String, Byte[]), SavePicture(String, Stream) - 保存图片到图片中心的“相册”,第一个参数是保存到媒体库的图片名称,第二个参数是需要被保存的图片数据
* SavePictureToCameraRoll(String, Byte[]), SavePictureToCameraRoll(String, Stream) - 保存图片到图片中心的“本机拍照”,第一个参数是保存到媒体库的图片名称,第二个参数是需要被保存的图片数据
* 注:关于 MediaLibrary 和 Picture 以及其他与媒体库(图片、音乐、播放列表等)相关的介绍详见 Microsoft.Xna.Framework.Media 命名空间下的类:http://msdn.microsoft.com/en-us/library/dd254868(v=xnagamestudio.40)
*
* PictureDecoder.DecodeJpeg(Stream source) - 解码 jpeg 格式文件到 WriteableBitmap 对象(经测试 png 格式也可以)
*
* WriteableBitmap - 位图 API,详见:http://www.cnblogs.com/webabcd/archive/2009/08/27/1554804.html 中的关于 WriteableBitmap 的介绍
* SaveJpeg() - 新增的扩展方法,用于将 WriteableBitmap 对象保存为 jpeg 格式文件
* LoadJpeg() - 新增的扩展方法,用于将 jpeg 格式图片加载到 WriteableBitmap 对象
*/ 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.Resources;
using System.Windows.Media.Imaging;
using Microsoft.Phone;
using Microsoft.Xna.Framework.Media;
using System.IO.IsolatedStorage;
using System.IO; namespace Demo.Media
{
public partial class WriteableBitmapDemo : PhoneApplicationPage
{
private WriteableBitmap _wb; public WriteableBitmapDemo()
{
InitializeComponent(); ShowImage();
} private void ShowImage()
{
Uri imageUri = new Uri("Assets/TileBackgroundBlue.png", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(imageUri); // 将图片流解码为 WriteableBitmap 对象,经测试不只是 jpeg 格式可以,png 格式也可以
_wb = PictureDecoder.DecodeJpeg(sri.Stream); // 将图片的第 10 行的像素点都改为红色
for (int i = _wb.PixelWidth * ; i < _wb.PixelWidth * ; i++)
{
unchecked
{
// 每个像素的颜色的描述规范为 ARGB
_wb.Pixels[i] = (int)0xFFFF0000;
}
} // 重新绘制整个 WriteableBitmap 对象
_wb.Invalidate(); // 显示修改后的图片
img.Source = _wb;
} // 将图片保存到图片 hub 的“本机拍照”
private void btnSaveToCameraRollAlbum_Click(object sender, RoutedEventArgs e)
{
// 在独立存储中创建一个临时文件
string fileName = "myImage.jpg";
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(fileName))
myStore.DeleteFile(fileName);
IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName); // 将图片保存到独立存储的临时文件
_wb.SaveJpeg(myFileStream, _wb.PixelWidth, _wb.PixelHeight, , );
myFileStream.Close(); // 打开独立存储中的图片
myFileStream = myStore.OpenFile(fileName, FileMode.Open, FileAccess.Read); // 将图片保存到“本机拍照”
MediaLibrary library = new MediaLibrary();
Picture pic = library.SavePictureToCameraRoll("SavedPicture.jpg", myFileStream);
} // 将图片保存到图片 hub 的“相册”
private void btnSaveToPictureAlbum_Click(object sender, RoutedEventArgs e)
{
// 在独立存储中创建一个临时文件
string fileName = "myImage.jpg";
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(fileName))
myStore.DeleteFile(fileName);
IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName); // 将图片保存到独立存储的临时文件
_wb.SaveJpeg(myFileStream, _wb.PixelWidth, _wb.PixelHeight, , );
myFileStream.Close(); // 打开独立存储中的图片
myFileStream = myStore.OpenFile(fileName, FileMode.Open, FileAccess.Read); // 将图片保存到“相册”
MediaLibrary library = new MediaLibrary();
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
}
}
}

2、演示如何与图片的上下文菜单“应用程序...”关联
IntegrateWithThePictureViewer.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.IntegrateWithThePictureViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock TextWrapping="Wrap">
<Run>在“图片中心”中查看某图片,然后点击 AppBar 的“应用程序...”按钮,则会发现本 app 也在选项列表中,也就是说可以使用本 app 打开指定的图片</Run>
<LineBreak />
<Run>具体实现方法请参见 manifest 和 MainPage.xaml.cs</Run>
</TextBlock>
</Grid> </phone:PhoneApplicationPage>

WMAppManifest.xml

<Extensions>
<!--
与图片的上下文菜单“应用程序...”关联,即在“图片中心”中查看某图片,然后点击 AppBar 的“应用程序...”按钮,则会发现本 app 也在选项列表中,也就是说可以使用本 app 打开指定的图片
-->
<Extension ExtensionName="Photos_Extra_Viewer" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" />
</Extensions>

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 当使用本 app 打开指定的图片时,会传递过来一个 token 参数
// 开发时要注意,如果同时使用了 PhotoChooserTask ,要避免冲突
if (NavigationContext.QueryString.ContainsKey("token"))
{
MediaLibrary library = new MediaLibrary();
// 根据 token 获取到指定的图片
Picture picture = library.GetPictureFromToken(NavigationContext.QueryString["token"]); // 将 Picture 对象转换成 BitmapImage 对象
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(picture.GetImage()); // 将 BitmapImage 对象转换成 WriteableBitmap 并显示
WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);
img.Source = picLibraryImage;
}
}

3、演示如何与图片的上下文菜单“共享...”关联
IntegrateWithThePictureShare.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.IntegrateWithThePictureShare"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock TextWrapping="Wrap">
<Run>在“图片中心”中查看某图片,然后点击 AppBar 的“共享...”按钮,则会发现本 app 也在选项列表中,也就是说可以使用本 app 共享指定的图片</Run>
<LineBreak />
<Run>具体实现方法请参见 manifest 和 MainPage.xaml.cs</Run>
</TextBlock>
</Grid> </phone:PhoneApplicationPage>

WMAppManifest.xml

<Extensions>
<!--
与图片的上下文菜单“共享...”关联,即在“图片中心”中查看某图片,然后点击 AppBar 的“共享...”按钮,则会发现本 app 也在选项列表中,也就是说可以使用本 app 共享指定的图片
-->
<Extension ExtensionName="Photos_Extra_Share" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" />
</Extensions>

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 当使用本 app 共享指定的图片时,会传递过来一个 FileId 参数
if (NavigationContext.QueryString.ContainsKey("FileId"))
{
MediaLibrary library = new MediaLibrary();
// 根据 FileId 获取到指定的图片
Picture picture = library.GetPictureFromToken(NavigationContext.QueryString["FileId"]); // 将 Picture 对象转换成 BitmapImage 对象
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(picture.GetImage()); // 将 BitmapImage 对象转换成 WriteableBitmap 并显示
WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);
img.Source = picLibraryImage;
}
}

4、演示如何与 Windows Phone 的图片中心集成
IntegrateWithThePictureHub.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.IntegrateWithThePictureHub"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock TextWrapping="Wrap">
<Run>本 app 会出现在“图片中心”中的“应用程序”下</Run>
<LineBreak />
<Run>具体实现方法请参见 manifest</Run>
</TextBlock>
</Grid> </phone:PhoneApplicationPage>

WMAppManifest.xml

<Extensions>
<!--
与 Windows Phone 的图片中心集成,即将本 app 添加到“图片中心”中的“应用程序”下
-->
<Extension ExtensionName="Photos_Extra_Hub" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" />
</Extensions>

OK
[源码下载]

与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成的更多相关文章

  1. mac通过wine运行windows程序(将文件关联到windows程序notepad++)

    windows程序功能强大且已经习惯使用了,用mac总感觉不给力,例如记事本工具Notepad++就非常优秀.下面介绍如何在mac系统下通过wine来安装使用notepadd++程序. 1.安装win ...

  2. 与众不同 windows phone (15) - Media(媒体)之后台播放音频

    原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页][源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音频 ...

  3. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

  4. CSS Media媒体查询使用大全,完整媒体查询总结

    前面的话 一说到响应式设计,肯定离不开媒体查询media.一般认为媒体查询是CSS3的新增内容,实际上CSS2已经存在了,CSS3新增了媒体属性和使用场景(IE8-浏览器不支持).本文将详细介绍媒体查 ...

  5. css3 media媒体查询器用法总结

    随着响应式设计模型的诞生,Web网站又要发生翻天腹地的改革浪潮,可能有些人会觉得在国内IE6用户居高不下的情况下,这些新的技术还不会广泛的蔓延下去,那你就错了,如今淘宝,凡客,携程等等公司都已经在大胆 ...

  6. css3 media媒体查询器用法总结 兼容ie8以下的方法

    总结一下响应式设计的核心CSS技术Media(媒体查询器)的用法. http://www.360doc.com/content/14/0704/06/10734150_391862769.shtml ...

  7. Windows + Ubuntu 16.04 双系统安装详细教程

    Windows + Ubuntu 16.04 双系统安装详细教程 2018年01月28日 16:43:19 flyyufenfei 阅读数:165619   发现了一篇好教程,果断转载了,以后用得着时 ...

  8. 控制页面打印的2种方法(css3的media媒体查询和window.print())

    在实际开发中,有时可能会有打印的需求.下面我总结了2种打印的方法,希望对各位小伙伴有所帮助. ①:直接用window.print()方法就可以打印整个页面,下面是一个小demo <!DOCTYP ...

  9. css3 media媒体查询器用法总结(附js兼容方法)

    css3 media媒体查询器用法总结 标签:class   代码   style   html   sp   src 随着响应式设计模型的诞生,Web网站又要发生翻天腹地的改革浪潮,可能有些人会觉得 ...

随机推荐

  1. wifi密码破解方法总结(含破解软件下载链接)

    眼下网上流行有非常多无线password的破解方法,总结起来最有用的还是这两种:第一种是Wirelessnetview+WinAirCrackPack软件组合,这个方法简单方便:另外一种就是大家熟悉的 ...

  2. 更换Winform 皮肤(下)----完全GDI+绘制

    skin皮肤和DLL程序及文件:下载 链接:http://www.cnblogs.com/DebugLZQ/archive/2013/04/15/3021659.html

  3. mysql如何开启远程连接

    链接地址:http://jingyan.baidu.com/article/046a7b3ed85f3ef9c27fa9dc.html 大家在公司工作中,经常会遇到mysql数据库存储于某个人的电脑上 ...

  4. java 发送邮件 email相关操作代码测试,生成复杂格式邮件,发送邮件相关操作

    项目源码下载:http://download.csdn.net/detail/liangrui1988/6720047 效果图: 相关代码: test1 package com.mail; impor ...

  5. 给VS自动添加注释

    找到类文件所在路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\ ...

  6. Windows的TCP协议参数

    注册表编辑器:regedit 表项:HKEY_LOCAL_MACHINE\SYSTEM\CurentControlSet\Services\Tcpip\Parameters 窗口扩大因子 & ...

  7. HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

    lca的做法还是非常明显的.简单粗暴, 只是不是正解.假设树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的.预计就是放在splay上剖分一 ...

  8. vbox要手动mount才能挂载windows的共享文件夹(好用,不用安装samba了)

    mount -t vboxsf BaiduShare /mnt/bdshare/ 我按照这篇文章成功: http://www.wuji8.com/meta/448016166.html 其它参考: h ...

  9. Linux的五个查找命令 [转]

    最近,我在学习Linux,下面是一些笔记. 使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序 ...

  10. js中使用jstl中得到的值

    jstl的标签会转化为服务器端的代码执行,而js代码则在客户端执行.        要在js中使用jstl并不是直接将jstl的value赋值给一个js的变量,而是要在jstl的value上加上&qu ...