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

简介: 原文:与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成[索引页][源码下载] 与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序.
原文: 与众不同 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 * 9; i < _wb.PixelWidth * 10; 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, 0, 85);
            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, 0, 85);
            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
[源码下载]

目录
相关文章
|
5月前
|
安全 Ubuntu iOS开发
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
242 3
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
|
7月前
|
Ubuntu Linux Windows
如何在Ubuntu系统中安装Wine,借此来运行Windows程序
熟悉的登录画面出现,在Ubuntu系统中扫描登录微信程序。
|
7月前
|
Unix Linux 编译器
解决在Windows平台上运行Golang程序时出现的syscall.SIGUSR1未定义错误。
通过这种结构,你的代码既可以在支持 SIGUSR1 信号的系统上正常工作,又可以在不支持这些信号的 Windows 系统上编译通过,确保跨平台的兼容性和功能的完整性。
275 0
|
8月前
|
Windows
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
235 2
|
9月前
|
Windows
Windows下版本控制器(SVN)-启动服务器端程序
Windows下版本控制器(SVN)-启动服务器端程序
342 4
|
10月前
|
安全 测试技术 Linux
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
319 2
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
|
10月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
200 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
218 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
12月前
|
存储 监控 安全
如何排查常见的 Windows 应用程序错误和崩溃
本文介绍了如何通过事件日志分析来诊断Windows应用程序错误和崩溃的根本原因。文章首先解释了应用错误的表现形式及常见事件ID(如1000、1001等),并分析了导致崩溃的原因,包括硬件问题(如存储不足、外部因素)和软件问题(如编码错误、数据损坏、.NET Framework兼容性)。接着,提供了几种故障排除方法,例如运行系统文件检查器(SFC)、执行干净启动、检查更新以及重新安装.NET Framework。最后,探讨了使用日志管理工具(如EventLog Analyzer)集中分析崩溃事件的功能,包括预置报表、时间轴分析、实时警报和自动化响应,帮助管理员高效解决应用问题。
1874 1
|
12月前
|
Windows
Windows程序的数字签名证书怎么申请
Windows程序的数字签名证书申请流程包括:准备企业资料(营业执照、税务登记证等),提交申请表及企业资料。经过初审、实名认证和二审后,等待1-5个工作日审核结果。审核通过后,CA机构颁发证书并通过邮件或邮寄方式发送。收到证书后按指南安装并使用签名工具对程序进行数字签名,确保软件完整性和可信度。注意证书有效期、管理和兼容性问题。

热门文章

最新文章