导入图片并保存到隔离存储WP7

时间:2011-09-30 19:23:23

标签: c# image windows-phone-7 storage

我目前正在开展一个项目,用户需要从照片库中选择图像并导入。使用以下代码,我可以导入图片,但我有几个问题。

  1. 导入时命名的图像是什么?
  2. 导入后的图片在哪里
  3. 是否可以在再次打开应用程序时保存该图像并重新加载(即使用隔离存储)
  4. 下载教程中的代码

    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));
    
                }
            }
        }
    }
    

1 个答案:

答案 0 :(得分: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();
            }
        }
    
相关问题