【WP7】照片与扩展性

 WP7运行编写照片扩展程序,让应用程序显示在相册中,应用程序把图片写入相册

添加命名空间

  using Microsoft.Xna.Framework.Media;

1、保存图片到CameraRoll

  MediaLibrary library = new MediaLibrary();         //用于访问媒体库
  BitmapImage bmp = (BitmapImage)image1.Source;    //获取Image空间的图片资源
    Uri uri = bmp.UriSource;
  StreamResourceInfo Sri = Application.GetResourceStream(uri);
  library.SavePicture("11.jpg", Sri.Stream);

 

2、保存图片到Album

      MediaLibrary library = new MediaLibrary();      
      BitmapImage bmp = (BitmapImage)image1.Source;
      Uri uri = bmp.UriSource;
      StreamResourceInfo Sri = Application.GetResourceStream(uri);
      library.SavePictureToCameraRoll("11.jpg", Sri.Stream);

 

3、连接扩展图片中心

打开应用程序清单文件 WMAppManifest.xml 并紧接着 Tokens 元素后面添加以下代码作为 App 元素的子元素。这样,应用程序便能够从图片查看器中启动 

<Extensions>
      <Extension ExtensionName="Photos_Extra_Hub" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" />
</Extensions>

 

4、连接扩展图片查看器

  -在程序清单中添加以下代码

<Extensions>
      <Extension ExtensionName="Photos_Extra_Viewer" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" />
</Extensions>

  -重写应用程序页面导航函数 OnNavigatedTo()

View Code
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;
        if (queryStrings.ContainsKey("token"))
        {
            MediaLibrary library = new MediaLibrary();
            //获取图片信息
            Picture picture = library.GetPictureFromToken(queryStrings["token"]);

            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(picture.GetImage());
            
            //下面对图片做相应的操作
        }
    }

 

5、连接扩展共享选取器

操作与上面类似

<Extensions>
      <Extension ExtensionName="Photos_Extra_Share" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" />
</Extensions>

同样重写 OnNavigatedTo() 函数

View Code
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;
        if (queryStrings.ContainsKey("FileId"))
        {
            MediaLibrary library = new MediaLibrary();
            //获取图片信息
            Picture picture = library.GetPictureFromToken(queryStrings["FileId"]);

            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(picture.GetImage());
            
            //下面对图片做相应的操作
        }
    }

 

posted @ 2012-10-31 16:51  bomo  阅读(408)  评论(0编辑  收藏  举报