【转】Windows Phone在隔离存储里存取图片文件

一共两个页面,第一个为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));

         }

     }

 }

效果如图所示:

【转】Windows Phone在隔离存储里存取图片文件

同时还新建了一个照片文件的类,从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在隔离存储里存取图片文件

【转】Windows Phone在隔离存储里存取图片文件

上一篇:Cocos Creator cc.Node.点击事件


下一篇:android boot.img