zoukankan      html  css  js  c++  java
  • 【转】Windows Phone在隔离存储里存取图片文件

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

     1  <!--ContentPanel - place additional content here-->
    
     2         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    
     3             <Image Name="PhotoImage" Height="342" HorizontalAlignment="Left" Margin="9,6,0,0"  Stretch="Fill" VerticalAlignment="Top" Width="441"/>
    
     4             <Button Name="SaveButton" Click="SaveButton_Click" Content="保存" Height="72" HorizontalAlignment="Left" Margin="7,482,0,0"  VerticalAlignment="Top" Width="441">
    
     5                 <Button.Background>
    
     6                     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    
     7                         <GradientStop Color="Black" Offset="0"/>
    
     8                         <GradientStop Color="#FFEB1818" Offset="1"/>
    
     9                     </LinearGradientBrush>
    
    10                 </Button.Background>
    
    11             </Button>
    
    12             <TextBlock Height="30" HorizontalAlignment="Left" Margin="27,442,0,0" Name="textBlock1" Text="输入照片名:" FontSize="25" VerticalAlignment="Top"/>
    
    13             <TextBox Name="PhotoName" Height="72" HorizontalAlignment="Left" Margin="165,424,0,0"  Text="" VerticalAlignment="Top" Width="284">
    
    14                 <TextBox.Background>
    
    15                     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    
    16                         <GradientStop Color="Black" Offset="0"/>
    
    17                         <GradientStop Color="#FFD01818" Offset="1"/>
    
    18                     </LinearGradientBrush>
    
    19                 </TextBox.Background>
    
    20             </TextBox>
    
    21             <Button Name="TakePhotoButton" Click="TakePhotoButton_Click" Content="拍照" Height="72" Margin="9,366,8,0" VerticalAlignment="Top">
    
    22                 <Button.Background>
    
    23                     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    
    24                         <GradientStop Color="Black" Offset="0"/>
    
    25                         <GradientStop Color="#FFD61212" Offset="1"/>
    
    26                     </LinearGradientBrush>
    
    27                 </Button.Background>
    
    28             </Button>
    
    29         </Grid>
    
    30     </Grid>
    
    31 
    
    32     <!--Sample code showing usage of ApplicationBar-->
    
    33     <phone:PhoneApplicationPage.ApplicationBar>
    
    34         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    
    35             <shell:ApplicationBarIconButton IconUri="/Images/appbar.feature.search.rest.png" Text="查看" Click="ApplicationBarIconButton_Click"/>
    
    36         </shell:ApplicationBar>
    
    37     </phone:PhoneApplicationPage.ApplicationBar>
    前台

    后台代码如下:

      

    MainPage.xaml.cs
    
     1 using System;
    
     2 using System.Collections.Generic;
    
     3 using System.Linq;
    
     4 using System.Net;
    
     5 using System.Windows;
    
     6 using System.Windows.Controls;
    
     7 using System.Windows.Documents;
    
     8 using System.Windows.Input;
    
     9 using System.Windows.Media;
    
    10 using System.Windows.Media.Animation;
    
    11 using System.Windows.Shapes;
    
    12 using Microsoft.Phone.Controls;
    
    13 using Microsoft.Phone.Tasks;
    
    14 using Microsoft.Phone;
    
    15 using System.IO;
    
    16 using System.IO.IsolatedStorage;
    
    17
    
    18 namespace 在隔离存储中存取照片
    
    19 {
    
    20     public partial class MainPage : PhoneApplicationPage
    
    21     {
    
    22         // Constructor
    
    23         string filename;
    
    24         byte[] _imageAsByte;
    
    25         public MainPage()
    
    26         {
    
    27             InitializeComponent();
    
    28         }
    
    29
    
    30         private void SaveButton_Click(object sender, RoutedEventArgs e)
    
    31         {
    
    32             filename = PhotoName.Text;
    
    33             if (filename != ""&&PhotoImage.Source!=null)
    
    34             {
    
    35                 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    
    36                 {
    
    37                     if (!store.FileExists(filename) || store.FileExists(filename) && MessageBox.Show("Overwrite the file?", "Question", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
    
    38                     {
    
    39                         using (var stream = store.CreateFile(filename))
    
    40                         {
    
    41                             stream.Write(_imageAsByte, 0, _imageAsByte.Length);
    
    42                         }
    
    43                         PhotoImage.Source = null;
    
    44                         PhotoName.Text = "";
    
    45                         NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
    
    46                     }
    
    47                 }
    
    48             }
    
    49             else
    
    50             {
    
    51                 MessageBox.Show("Sorry,but you must take a photo and write the photo's name in the textbox!","Warning!",MessageBoxButton.OK);
    
    52             }
    
    53         }
    
    54
    
    55         private void TakePhotoButton_Click(object sender, RoutedEventArgs e)
    
    56         {
    
    57             CameraCaptureTask task = new CameraCaptureTask();
    
    58             task.Completed += new EventHandler<PhotoResult>(task_Completed);
    
    59             task.Show();
    
    60         }
    
    61
    
    62         void task_Completed(object sender, PhotoResult e)
    
    63         {
    
    64             if (e.TaskResult == TaskResult.OK)
    
    65             {
    
    66                 _imageAsByte = new byte[e.ChosenPhoto.Length];
    
    67                 e.ChosenPhoto.Read(_imageAsByte, 0, _imageAsByte.Length);
    
    68                 e.ChosenPhoto.Seek(0, SeekOrigin.Begin);
    
    69                 this.PhotoImage.Source = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
    
    70             }
    
    71         }
    
    72
    
    73         private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    
    74         {
    
    75             NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relative));
    
    76         }
    
    77     }
    
    78 }

    效果如图所示:

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

      

    PhotoFile.cs
    
     1 using System;
    
     2 using System.Net;
    
     3 using System.Windows;
    
     4 using System.Windows.Controls;
    
     5 using System.Windows.Documents;
    
     6 using System.Windows.Ink;
    
     7 using System.Windows.Input;
    
     8 using System.Windows.Media;
    
     9 using System.Windows.Media.Animation;
    
    10 using System.Windows.Shapes;
    
    11
    
    12 namespace 在隔离存储中存取照片
    
    13 {
    
    14     public class PhotoFile
    
    15     {
    
    16         public ImageSource Image { get; set; }
    
    17         public string ImageName { get; set; }
    
    18     }
    
    19 }

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

    1  <!--ContentPanel - place additional content here-->
    
     2         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    
     3             <ListBox Height="550" HorizontalAlignment="Left" Margin="6,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="460">
    
     4                 <ListBox.ItemTemplate>
    
     5                     <DataTemplate>
    
     6                         <Grid Margin="{StaticResource PhoneTouchTargetOverhang}">
    
     7                             <StackPanel Orientation="Horizontal">
    
     8                                 <Image Source="{Binding Image}" Width="130" Height="120" Stretch="Fill"/>
    
     9                                 <TextBlock Text="{Binding ImageName}" Foreground="Coral" FontSize="32" TextWrapping="Wrap" Width="300"/>
    
    10                             </StackPanel>
    
    11                         </Grid>
    
    12                     </DataTemplate>
    
    13                 </ListBox.ItemTemplate>
    
    14             </ListBox>
    
    15         </Grid>
    
    16     </Grid>
    
    17 
    
    18     <!--Sample code showing usage of ApplicationBar-->
    
    19     <phone:PhoneApplicationPage.ApplicationBar>
    
    20         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    
    21             <shell:ApplicationBarIconButton IconUri="/Images/appbar.add.rest.png" Text="添加" Click="ApplicationBarIconButton_Click"/>
    
    22         </shell:ApplicationBar>
    
    23     </phone:PhoneApplicationPage.ApplicationBar>

    后台代码:

      

    Page1.xaml.cs
    
     1 using System;
    
     2 using System.Collections.Generic;
    
     3 using System.Linq;
    
     4 using System.Net;
    
     5 using System.Windows;
    
     6 using System.Windows.Controls;
    
     7 using System.Windows.Documents;
    
     8 using System.Windows.Input;
    
     9 using System.Windows.Media;
    
    10 using System.Windows.Media.Animation;
    
    11 using System.Windows.Shapes;
    
    12 using Microsoft.Phone.Controls;
    
    13 using System.IO;
    
    14 using System.IO.IsolatedStorage;
    
    15 using System.Windows.Media.Imaging;
    
    16
    
    17 namespace 在隔离存储中存取照片
    
    18 {
    
    19     public partial class Page1 : PhoneApplicationPage
    
    20     {
    
    21         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
    
    22         public Page1()
    
    23         {
    
    24             InitializeComponent();
    
    25         }
    
    26
    
    27        protected override void OnNavigatedTo(NavigationEventArgs e)
    
    28         {
    
    29             List<PhotoFile> filelist = new List<PhotoFile>();
    
    30             if (file.GetFileNames() != null)
    
    31             {
    
    32                 foreach (string filename in file.GetFileNames())
    
    33                 {
    
    34                     PhotoFile photo = new PhotoFile();
    
    35                     BitmapImage bmp = new BitmapImage();
    
    36                     using (IsolatedStorageFileStream filestream = file.OpenFile(filename, FileMode.Open, FileAccess.ReadWrite))
    
    37                     {
    
    38                         bmp.SetSource(filestream);
    
    39                         filestream.Flush();
    
    40                         filestream.Close();
    
    41                         photo.Image = bmp;
    
    42                         photo.ImageName = " 照片名:"+filename;
    
    43                     }
    
    44                     filelist.Add(photo);
    
    45                 }
    
    46                 listBox1.ItemsSource = filelist;
    
    47             }
    
    48         }
    
    49
    
    50         private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    
    51         {
    
    52             NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative));
    
    53         }
    
    54     }
    
    55 }

    运行效果如下:

  • 相关阅读:
    Jmeter_完成文件批量下载
    Jmeter_文件的下载
    Jmeter_针对一个账户批量上传文件
    Jmeter上传文件操作
    Jmeter_获取结果写到excel
    Jmeter_接口串联自动化测试_登录后充值获取cookie
    JS 对象(Object)和字符串(String)互转
    全国图书馆参考咨询联盟关闭文献传递功能解决办法
    Geodesic Distance:两点间的最短距离之法截弧/等角航线/测地线
    空间数据库基础理论 GIS空间数据处理分析涉及的基本概念
  • 原文地址:https://www.cnblogs.com/ZXdeveloper/p/3582987.html
Copyright © 2011-2022 走看看