windows Phone 7 保存图片到MediaLibrary

来源:互联网 发布:设备优化改善报告ppt 编辑:程序博客网 时间:2024/05/04 20:33

windows phone for silverlight 项目中不能直接把图片,视频,音频等文件放到MediaLibray 中,不过XNA的库里可以实现这个功能。

1.首先要引用XNA的库 :
using Microsoft.Xna.Framework.Media;

2.定义一个MediaLibrary对象,指定图片文件.

MediaLibrary library = new MediaLibrary();

string lName ="/Images/image1.jpg";


3. 声明一个存储文件流,检查文件是否存在. 
   var myStore = IsolatedStorageFile.GetUserStoreForApplication();


   if (myStore.FileExists(lName))
   {
          MessageBox.Show("图片已经存在");
   }
   else
   {

4.将图片文件存储在独立存储,将其转码最后存储在媒体库(MediaLibrary)里.

         IsolatedStorageFileStream myFileStream = myStore.CreateFile(lName);
         BitmapImage image = new BitmapImage();
         image.SetSource(e.Result);
         WriteableBitmap CaptureImage = new WriteableBitmap(image);

        // 将WriteableBitmap转换为JPEG流编码,并储存到独立存储里.
        Extensions.SaveJpeg(CaptureImage, myFileStream, CaptureImage.PixelWidth, CaptureImage.PixelHeight, 0, 85);
        myFileStream.Close();               // 关闭文件流.

       //从独立存储里读出刚存入的图片文件.
       myFileStream = myStore.OpenFile(lName, FileMode.Open, FileAccess.Read);

       //把图片加在WP7 手机的媒体库.
       Picture pic = library.SavePicture(lName, myFileStream);
       myFileStream.Close();
       MessageBox.Show("保存成功!", "提示", MessageBoxButton.OK);

}


默认的媒体库:   


保存后效果图(如Saved Pictures 所示):

原创粉丝点击