本帖最后由 agameboy 于 2012-5-17 17:08 编辑
对于很多应用,向隔离存储空间读取、保存图片文件是很常见的任务。在WP7中,你还可以保存、读取媒体库中的图片。更多的文章请参考WP7 IsolatedStorage系列篇! 引用命名空间: using System.IO; using System.IO.IsolatedStorage; using System.Windows.Media.Imaging; using System.Windows.Resources; using Microsoft.Phone.Tasks; using Microsoft.Xna.Framework.Media;
关心:Microsoft.Xna.Framework.Media;仅当你要把图片保存到媒体库的时候才需要添加引用。 一般情况下我们使用类IsolatedStorageFileStream进行读、写、创建文件等操作。对于图片,最大的不同就是使用类BitmapImage和类WriteableBitmap.
保存Image:
- private void btSaveImage_Click(object sender, RoutedEventArgs e)
- {
- String strTempJPEG = "iamge111.png";
- using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
- {
- if (iso.FileExists (strTempJPEG ))
- {
- iso.DeleteFile(strTempJPEG );
- }
- using(IsolatedStorageFileStream isostream=iso.CreateFile(strTempJPEG))
- {
- StreamResourceInfo sri = null;
- Uri uri = new Uri(strTempJPEG ,UriKind.Relative);
- sri = Application.GetResourceStream(uri);
- BitmapImage bitmap = new BitmapImage();
- bitmap.SetSource(sri.Stream );
- WriteableBitmap wb = new WriteableBitmap(bitmap);
- Extensions.SaveJpeg(wb ,isostream,wb.PixelWidth,wb.PixelHeight,0,85);
- isostream.Close();
- }
- }
- }
复制代码
读取Image:
- private void btScanImage_Click(object sender, RoutedEventArgs e)
- {
- BitmapImage bitmap = new BitmapImage();
- using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
- {
- using(IsolatedStorageFileStream isostream=iso.OpenFile ("iamge111.png",FileMode.Open ,FileAccess.Read ))
- {
- bitmap.SetSource(isostream);
- this.image.Height = bitmap.PixelHeight;
- this.image.Width = bitmap.PixelWidth;
- }
- }
- this.image.Source = bitmap;
- }
复制代码
|