WP7 Isolated Storage详解 读取、保存图片文件
2011-12-27 16:31
369 查看
首先创建一个Windows Phone 7项目,在项目中添加一个图片例如“logo.jpg”,然后在MainPage.xaml.cs(或其他页面文件)中引入命名空间:
using System.IO.IsolatedStorage; using System.Windows.Media.Imaging; using System.IO; using System.Windows.Resources; using Microsoft.Xna.Framework.Media; using Microsoft.Phone.Tasks;
提示:Microsoft.Xna.Framework.Media;仅当你要把图片保存到媒体库的时候才需要添加引用。
对于很多应用,向隔离存储空间读取、保存图片文件是很常见的任务。在WP7中,你还可以保存、读取媒体库中的图片。
一般情况下我们使用类IsolatedStorageFileStream进行读、写、创建文件等操作。对于图片,最大的不同就是使用类BitmapImage和类WriteableBitmap.
保存图片到隔离存储空间
示例中首先检查文件是否已经存在,然后把图片保存到隔离存储空间(转换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控件中呈现出来。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。示例中从隔离存储空间中读取一个图片,然后保存到手机的媒体库中。
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();
相关文章推荐
- WP7 Isolated Storage详解(7)-读取、保存图片文件
- WP7 Isolated Storage详解(7)-读取、保存图片文件
- WP7 IsolatedStorage--读取、保存图片文件
- WP7 IsolatedStorage--读取、保存图片文件
- 利用Python PIL、cPickle将图片读取和保存为pkl格式文件
- C编写以二进制读取文件(任意格式eg :图片),保存到C语言数据格式的TXT文档
- 读取资源图片保存在本地解决文件无法创建问题。
- matlab:读取mp4文件,并把逐帧保存成图片文件,文件名的编号随帧数变化
- HALCON初步:文件夹遍历,文件筛选,文件名拆分,图片读取及保存
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 读取文件中的所有图片的路径,保存到txt文件中。
- WP7 IsolatedStorage--读取、保存二进制文件
- HALCON初步:文件夹遍历,文件筛选,文件名拆分,图片读取及保存
- iphone Documents文件中保存的图片如何读取并显示?
- 批量读取图片保存为csv文件
- Android读取url图片保存及文件读取
- 在Java中读取并保存EXCEL文件中图片
- 从数据库中读取一个图片并保存为一个图片文件
- C++读取与保存bmp图片文件编程实现
- 【EMGU】读取视频AVI文件+保存任意帧为图片