【轉】Windows Phone在隔離儲存裡存取圖片檔案

來源:互聯網
上載者:User

標籤:.sh   hang   null      begin   etc   files   ntp   cti   

一共兩個頁面,第一個為MainPage.xaml,代碼如下:

 前台

 

後台代碼如下:

  

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;1718 namespace 在隔離儲存中存取照片19 {20     public partial class MainPage : PhoneApplicationPage21     {22         // Constructor23         string filename;24         byte[] _imageAsByte;25         public MainPage()26         {27             InitializeComponent();28         }2930         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             else50             {51                 MessageBox.Show("Sorry,but you must take a photo and write the photo‘s name in the textbox!","Warning!",MessageBoxButton.OK);52             }53         }5455         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         }6162         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         }7273         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;1112 namespace 在隔離儲存中存取照片13 {14     public class PhotoFile15     {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;1617 namespace 在隔離儲存中存取照片18 {19     public partial class Page1 : PhoneApplicationPage20     {21         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();22         public Page1()23         {24             InitializeComponent();25         }2627        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         }4950         private void ApplicationBarIconButton_Click(object sender, EventArgs e)51         {52             NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative));53         }54     }55 }

 

運行效果如下:

 

【轉】Windows Phone在隔離儲存裡存取圖片檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.