一共两个页面,第一个为MainPage.xaml,代码如下:

   <!--ContentPanel - place additional content here-->

          <Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0">

              <Image Name="PhotoImage" Height="" HorizontalAlignment="Left" Margin="9,6,0,0"  Stretch="Fill" VerticalAlignment="Top" Width=""/>

              <Button Name="SaveButton" Click="SaveButton_Click" Content="保存" Height="" HorizontalAlignment="Left" Margin="7,482,0,0"  VerticalAlignment="Top" Width="">

                  <Button.Background>

                      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                          <GradientStop Color="Black" Offset=""/>

                          <GradientStop Color="#FFEB1818" Offset=""/>

                      </LinearGradientBrush>

                 </Button.Background>

             </Button>

             <TextBlock Height="" HorizontalAlignment="Left" Margin="27,442,0,0" Name="textBlock1" Text="输入照片名:" FontSize="" VerticalAlignment="Top"/>

             <TextBox Name="PhotoName" Height="" HorizontalAlignment="Left" Margin="165,424,0,0"  Text="" VerticalAlignment="Top" Width="">

                 <TextBox.Background>

                     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                         <GradientStop Color="Black" Offset=""/>

                         <GradientStop Color="#FFD01818" Offset=""/>

                     </LinearGradientBrush>

                 </TextBox.Background>

             </TextBox>

             <Button Name="TakePhotoButton" Click="TakePhotoButton_Click" Content="拍照" Height="" Margin="9,366,8,0" VerticalAlignment="Top">

                 <Button.Background>

                     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                         <GradientStop Color="Black" Offset=""/>

                         <GradientStop Color="#FFD61212" Offset=""/>

                     </LinearGradientBrush>

                 </Button.Background>

             </Button>

         </Grid>

     </Grid>

     <!--Sample code showing usage of ApplicationBar-->

     <phone:PhoneApplicationPage.ApplicationBar>

         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">

             <shell:ApplicationBarIconButton IconUri="/Images/appbar.feature.search.rest.png" Text="查看" Click="ApplicationBarIconButton_Click"/>

         </shell:ApplicationBar>

     </phone:PhoneApplicationPage.ApplicationBar>

前台

后台代码如下:

MainPage.xaml.cs

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Net;

  using System.Windows;

  using System.Windows.Controls;

  using System.Windows.Documents;

  using System.Windows.Input;

  using System.Windows.Media;

 using System.Windows.Media.Animation;

 using System.Windows.Shapes;

 using Microsoft.Phone.Controls;

 using Microsoft.Phone.Tasks;

 using Microsoft.Phone;

 using System.IO;

 using System.IO.IsolatedStorage;

 namespace 在隔离存储中存取照片

 {

     public partial class MainPage : PhoneApplicationPage

     {

         // Constructor

         string filename;

         byte[] _imageAsByte;

         public MainPage()

         {

             InitializeComponent();

         }

         private void SaveButton_Click(object sender, RoutedEventArgs e)

         {

             filename = PhotoName.Text;

             if (filename != ""&&PhotoImage.Source!=null)

             {

                 using (var store = IsolatedStorageFile.GetUserStoreForApplication())

                 {

                     if (!store.FileExists(filename) || store.FileExists(filename) && MessageBox.Show("Overwrite the file?", "Question", MessageBoxButton.OKCancel) == MessageBoxResult.OK)

                     {

                         using (var stream = store.CreateFile(filename))

                         {

                             stream.Write(_imageAsByte, , _imageAsByte.Length);

                         }

                         PhotoImage.Source = null;

                         PhotoName.Text = "";

                         NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));

                     }

                 }

             }

             else

             {

                 MessageBox.Show("Sorry,but you must take a photo and write the photo's name in the textbox!","Warning!",MessageBoxButton.OK);

             }

         }

         private void TakePhotoButton_Click(object sender, RoutedEventArgs e)

         {

             CameraCaptureTask task = new CameraCaptureTask();

             task.Completed += new EventHandler<PhotoResult>(task_Completed);

             task.Show();

         }

         void task_Completed(object sender, PhotoResult e)

         {

             if (e.TaskResult == TaskResult.OK)

             {

                 _imageAsByte = new byte[e.ChosenPhoto.Length];

                 e.ChosenPhoto.Read(_imageAsByte, , _imageAsByte.Length);

                 e.ChosenPhoto.Seek(, SeekOrigin.Begin);

                 this.PhotoImage.Source = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

             }

         }

         private void ApplicationBarIconButton_Click(object sender, EventArgs e)

         {

             NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relative));

         }

     }

 }

效果如图所示:

同时还新建了一个照片文件的类,从IsolatedStorage里读取出照片及照片名再绑定到Page1.xaml中的ListBox中。类代码如下:

PhotoFile.cs

  using System;

  using System.Net;

  using System.Windows;

  using System.Windows.Controls;

  using System.Windows.Documents;

  using System.Windows.Ink;

  using System.Windows.Input;

  using System.Windows.Media;

  using System.Windows.Media.Animation;

 using System.Windows.Shapes;

 namespace 在隔离存储中存取照片

 {

     public class PhotoFile

     {

         public ImageSource Image { get; set; }

         public string ImageName { get; set; }

     }

 }

第二个Page1.xaml,代码如下:

  <!--ContentPanel - place additional content here-->

          <Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0">

              <ListBox Height="" HorizontalAlignment="Left" Margin="6,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="">

                  <ListBox.ItemTemplate>

                      <DataTemplate>

                          <Grid Margin="{StaticResource PhoneTouchTargetOverhang}">

                              <StackPanel Orientation="Horizontal">

                                  <Image Source="{Binding Image}" Width="" Height="" Stretch="Fill"/>

                                  <TextBlock Text="{Binding ImageName}" Foreground="Coral" FontSize="" TextWrapping="Wrap" Width=""/>

                             </StackPanel>

                         </Grid>

                     </DataTemplate>

                 </ListBox.ItemTemplate>

             </ListBox>

         </Grid>

     </Grid>

     <!--Sample code showing usage of ApplicationBar-->

     <phone:PhoneApplicationPage.ApplicationBar>

         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">

             <shell:ApplicationBarIconButton IconUri="/Images/appbar.add.rest.png" Text="添加" Click="ApplicationBarIconButton_Click"/>

         </shell:ApplicationBar>

     </phone:PhoneApplicationPage.ApplicationBar>

后台代码:

Page1.xaml.cs

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Net;

  using System.Windows;

  using System.Windows.Controls;

  using System.Windows.Documents;

  using System.Windows.Input;

  using System.Windows.Media;

 using System.Windows.Media.Animation;

 using System.Windows.Shapes;

 using Microsoft.Phone.Controls;

 using System.IO;

 using System.IO.IsolatedStorage;

 using System.Windows.Media.Imaging;

 namespace 在隔离存储中存取照片

 {

     public partial class Page1 : PhoneApplicationPage

     {

         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

         public Page1()

         {

             InitializeComponent();

         }

        protected override void OnNavigatedTo(NavigationEventArgs e)

         {

             List<PhotoFile> filelist = new List<PhotoFile>();

             if (file.GetFileNames() != null)

             {

                 foreach (string filename in file.GetFileNames())

                 {

                     PhotoFile photo = new PhotoFile();

                     BitmapImage bmp = new BitmapImage();

                     using (IsolatedStorageFileStream filestream = file.OpenFile(filename, FileMode.Open, FileAccess.ReadWrite))

                     {

                         bmp.SetSource(filestream);

                         filestream.Flush();

                         filestream.Close();

                         photo.Image = bmp;

                         photo.ImageName = " 照片名:"+filename;

                     }

                     filelist.Add(photo);

                 }

                 listBox1.ItemsSource = filelist;

             }

         }

         private void ApplicationBarIconButton_Click(object sender, EventArgs e)

         {

             NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative));

         }

     }

 }

运行效果如下:

【转】Windows Phone在隔离存储里存取图片文件的更多相关文章

  1. [深入浅出WP8.1(Runtime)]生成图片和存储生成的图片文件

    7.2.3 使用RenderTargetBitmap类生成图片 RenderTargetBitmap类可以将可视化对象转换为位图,也就是说它可以将任意的UIElement以位图的形式呈现.那么我们在实 ...

  2. 如何在mysql中存储音乐和图片文件

    如何在mysql中存储音乐和图片文件? 果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的! 我将告诉你怎样通过HTML表单来储存这些文件, ...

  3. Windows Phone开发(29):隔离存储C

    原文:Windows Phone开发(29):隔离存储C 本文是隔离存储的第三节,大家先喝杯咖啡放松,今天的内容也是非常简单,我们就聊一件东东--用户设置. 当然了,可能翻译为应用程序设置合适一些,不 ...

  4. Windows Phone开发(28):隔离存储B

    原文:Windows Phone开发(28):隔离存储B 上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件. 首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的, ...

  5. Windows Phone开发(27):隔离存储A

    原文:Windows Phone开发(27):隔离存储A 在很多资料或书籍上都翻译为"独立存储",不过,我想了一下,决定将IsolatedStorage翻译为"隔离存储& ...

  6. Windows里面的hosts文件

    一.什么是Hosts文件? hosts文件是一个用于储存计算机网络中各节点信息的计算机文件.这个文件负责将主机名映射到相应的IP地址.hosts文件通常用于补充或取代网络中DNS的功能.和DNS不同的 ...

  7. windows下部署 ISCSI存储

    Write bt xiaoyang 配置篇 这里使用的软件为iscsiTargetqfe 1.      首先安装软件,可在微软官网下载 2.      然后找到安装程序 3.      完成安装后打 ...

  8. python一些模块的exe安装包在windows的64位系统里识别不到已安装Python目录的解决方法

    在windows里安装python一些模块时候,有时候源码安装比较困难,pip install也各种报错,这时候最喜欢用别人编译好的exe或者whl文件来安装,但是在windows的64位系统里,如果 ...

  9. 引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下。

    引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下. ...

随机推荐

  1. new的原罪

    一直以为在开发阶段能够直接调用的,速度而言一定是最优秀的,因为总比后期通过反射之类来调用来得快吧. 下面请看一个SB的例子,重新编译以后,这个类在创建100,000,000实体时居然耗费了16秒的时间 ...

  2. 网页的Width ,Height

    Jquery中可直接用接口$().height(); 获取浏览器窗口高$(window).height() 获取内部文档高$("body").height() 原生JS 网页可见区 ...

  3. 剑指offer算法_位运算求和

    不用+,-,*,/运算求和,可以分成三步: 1.计算两个数字的异或值,相当于只计算每一位的和,不计算进位,得出结果sum: 2.计算两个数字的与值,相当于求出两个数字的进位,然后左移一位,相当于进位, ...

  4. rdc21n(研发与设计综合讨论)博客开通了!

    rdc21n是“Research and Design Comprehensive discussioN”,其中21表示Comprehensive discussioN中间的21个字母(不包含空格), ...

  5. js继承理解(有引用)

    具体栗子:http://www.cnblogs.com/ayqy/p/4471638.html(地下为自己理解总结,可能晦涩,链接详细) 1.父类实例赋予子类原型. 2.利用子类函数引用 父类.cal ...

  6. ecshop数据表说明

    ecshop数据表说明 数据库结构说明,以及自己在后台备份不需要备份的表(红色字体是不需要备份的表)备份文件在FTP或者服务器上网站根目录下data\sqldata下 数据库采用mysql,共78张表 ...

  7. .Net mvc 后台传单引号错误&#39

    今天调试半天程序 结果出现JS 无法eval() 网上找个半天解决办法无果 最后 是因为后台输出单引号’ 到前台为&#39 解决办法 给一个隐藏文本框或者标签赋值 再取出来就能显示单引号了

  8. angularjs 时间格式化

    本地化日期格式化: ({{ today | date:'medium' }})Nov 19, 2015 3:57:48 PM ({{ today | date:'short' }})11/19/15 ...

  9. VC++ 19 (VS2015) 编译器系统环境变量配置

    Visual C++的cl.exe编译器是微软推出的编译器,干什么的怎么用也不赘述了.大多数情况都是直接在Visual Studio里写代码然后点击"播放"按钮让Visual St ...

  10. EntityFunctions.AsNonUnicode

    http://blog.csdn.net/zzx3q/article/details/7863797 使用工具VS2010 凡是调用FindAll的地方,如果传入参数是String类型的变量(数字类型 ...