zoukankan      html  css  js  c++  java
  • WP7 Isolated Storage详解(7)读取、保存图片文件

    转载自:http://www.cnblogs.com/zdave/archive/2011/06/01/2067282.html

    首先创建一个Windows Phone 7项目,在项目中添加一个图片例如“logo.jpg”,然后在MainPage.xaml.cs(或其他页面文件)中引入命名空间:

    1 using System.IO.IsolatedStorage;
    2 using System.Windows.Media.Imaging;
    3 using System.IO;
    4 using System.Windows.Resources;
    5 using Microsoft.Xna.Framework.Media;
    6 using Microsoft.Phone.Tasks;

    提示:Microsoft.Xna.Framework.Media;仅当你要把图片保存到媒体库的时候才需要添加引用。

    对于很多应用,向隔离存储空间读取、保存图片文件是很常见的任务。在WP7中,你还可以保存、读取媒体库中的图片。

    一般情况下我们使用类IsolatedStorageFileStream进行读、写、创建文件等操作。对于图片,最大的不同就是使用类BitmapImage和类WriteableBitmap.

    保存图片到隔离存储空间

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

    01 String tempJPEG = "logo.jpg";
    02  
    03 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    04 {
    05     if (myIsolatedStorage.FileExists(tempJPEG))
    06     {
    07         myIsolatedStorage.DeleteFile(tempJPEG);
    08     }
    09  
    10     IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
    11  
    12     StreamResourceInfo sri = null;
    13     Uri uri = new Uri(tempJPEG, UriKind.Relative);
    14     sri = Application.GetResourceStream(uri);
    15  
    16     BitmapImage bitmap = new BitmapImage();
    17     bitmap.SetSource(sri.Stream);
    18     WriteableBitmap wb = new WriteableBitmap(bitmap);
    19  
    20     // Encode WriteableBitmap object to a JPEG stream.
    21     Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
    22  
    23     fileStream.Close();
    24 }

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

    从隔离存储空间读取图片

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

    01 BitmapImage bi = new BitmapImage();
    02   
    03             using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    04             {
    05                 using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
    06                 {
    07                     bi.SetSource(fileStream);
    08                     this.img.Height = bi.PixelHeight;
    09                     this.img.Width = bi.PixelWidth;
    10                 }
    11             }
    12             this.img.Source = bi;

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

    保存图片到手机媒体库

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

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

    01 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    02             {
    03                 using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
    04                 {
    05                     MediaLibrary mediaLibrary = new MediaLibrary();
    06                     Picture pic = mediaLibrary.SavePicture("SavedLogo.jpg", fileStream);
    07                     fileStream.Close();
    08                 }
    09             }
    10   
    11             PhotoChooserTask photoChooserTask = new PhotoChooserTask();
    12             photoChooserTask.Show();
  • 相关阅读:
    leetcode204-统计质数个数之一步步调试超时
    SpringBoot-注解一句话
    算法-总结规律
    kafka-版本变更相关
    异步线程池如何做同步业务
    es-快捷DSL检索手记
    并发学习第七篇——ThreadPoolExecutor
    kafka-consumer端的设计细节
    kafka-producer使用总结
    kafka-topic重要配置分析
  • 原文地址:https://www.cnblogs.com/jeekun/p/2067600.html
Copyright © 2011-2022 走看看