2011-09-30 157 views
2

我目前正在開發一個項目,用戶需要從那裏從照片庫中選擇圖片並導入它。使用下面的代碼我可以導入圖片,但我有幾個問題。導入圖片並保存到獨立存儲WP7

  1. 什麼是導入時命名的圖像?
  2. 哪裏是位於圖像導入一次
  3. 是否有可能保存圖像,並重新加載它,當應用程序被再次打開(即使用獨立存儲)

繼承人的代碼從一個教程

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 System.IO; 
using System.Windows.Media.Imaging; 

namespace PhoneApp4 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 
     PhotoChooserTask selectphoto = null; 
     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      selectphoto = new PhotoChooserTask(); 
      selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed); 
      selectphoto.Show(); 

     } 

     void selectphoto_Completed(object sender, PhotoResult e) 
     { 
      if (e.TaskResult == TaskResult.OK) 
      { 

       BinaryReader reader = new BinaryReader(e.ChosenPhoto); 
       image1.Source = new BitmapImage(new Uri(e.OriginalFileName)); 

      } 
     } 
    } 
} 

回答

2
  1. PhotoResult包含OriginalFileName。
  2. 當PhotoChoserTask完成時PhotoResult.ChosenPhoto爲您提供了照片數據流。
  3. 是的,那時你可以將圖像存儲在獨立的存儲器中。

    private void Pick_Click(object sender, RoutedEventArgs e) 
        { 
         var pc = new PhotoChooserTask(); 
         pc.Completed += pc_Completed; 
         pc.Show(); 
        } 
    
        void pc_Completed(object sender, PhotoResult e) 
        { 
         var originalFilename = Path.GetFileName(e.OriginalFileName); 
         SaveImage(e.ChosenPhoto, originalFilename, 0, 100); 
        } 
    
        public static void SaveImage(Stream imageStream, string fileName, int orientation, int quality) 
        { 
         using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
         { 
          if (isolatedStorage.FileExists(fileName)) 
           isolatedStorage.DeleteFile(fileName); 
    
          var fileStream = isolatedStorage.CreateFile(fileName); 
          var bitmap = new BitmapImage(); 
          bitmap.SetSource(imageStream); 
    
          var wb = new WriteableBitmap(bitmap); 
          wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality); 
          fileStream.Close(); 
         } 
        } 
    
+0

如何訪問該文件名?而且我將如何去存儲它?你碰巧有一個教程或參考你會建議? – Al3xhamilton

+0

我添加了一些代碼。 –

+0

好的抱歉提出更多問題,但我如何訪問文件名,如果我把SaveImage代碼放在onexit類下,它會保存嗎?非常感謝 – Al3xhamilton