2011-09-30 153 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