WP7 Isolated Storage详解-读取、保存图片文件(转载)

转载自: http://www.zdave.net/archives/369

保存图片到隔离存储空间

示例中首先检查文件是否已经存在,然后把图片保存到隔离存储空间(转换WriteableBitmap对象到JPEG stream)。

String tempJPEG = "logo.jpg" ;
 
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
     if (myIsolatedStorage.FileExists(tempJPEG))
     {
         myIsolatedStorage.DeleteFile(tempJPEG);
     }
 
     IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
 
     StreamResourceInfo sri = null ;
     Uri uri = new Uri(tempJPEG, UriKind.Relative);
     sri = Application.GetResourceStream(uri);
 
     BitmapImage bitmap = new BitmapImage();
     bitmap.SetSource(sri.Stream);
     WriteableBitmap wb = new WriteableBitmap(bitmap);
 
     // Encode WriteableBitmap object to a JPEG stream.
     Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
 
     fileStream.Close();
}
 

提示:你也可以使用WriteableBitmap来保存图片:wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

从隔离存储空间读取图片

示例中首先从隔离存储空间打开了一个名为logo.jpg的图片,并在一个Image控件中呈现出来。

 
1
2
3
4
5
6
7
8
9
10
11
12
BitmapImage bi = new BitmapImage();
  
             using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile( "logo.jpg" , FileMode.Open, FileAccess.Read))
                 {
                     bi.SetSource(fileStream);
                     this .img.Height = bi.PixelHeight;
                     this .img.Width = bi.PixelWidth;
                 }
             }
             this .img.Source = bi;

提示:“img”是一个在MainPage.xaml中的图片控件:<Image x:Name="img"/>

保存图片到手机媒体库

访问MediaLibrary需要在项目中添加引用:Microsoft.XNA.Framework。

示例中从隔离存储空间中读取一个图片,然后保存到手机的媒体库中。

 
1
2
3
4
5
6
7
8
9
10
11
12
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile( "logo.jpg" , FileMode.Open, FileAccess.Read))
                 {
                     MediaLibrary mediaLibrary = new MediaLibrary();
                     Picture pic = mediaLibrary.SavePicture( "SavedLogo.jpg" , fileStream);
                     fileStream.Close();
                 }
             }
  
             PhotoChooserTask photoChooserTask = new PhotoChooserTask();
             photoChooserTask.Show();

 

转载于:https://www.cnblogs.com/asnowTT/articles/2279106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值