【问题标题】:Import picture and save to isolated storage WP7导入图片并保存到隔离存储 WP7
【发布时间】:2011-11-28 17:29:52
【问题描述】:

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

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

            }
        }
    }
}

【问题讨论】:

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


    【解决方案1】:
    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();
              }
          }
      

    【讨论】:

    • 如何访问该文件名?还有我将如何存储它?你有推荐的教程或参考资料吗?
    • 好的,很抱歉问了更多问题,但是我如何访问文件名,如果我将 SaveImage 代码放在 onexit 类下,它会保存吗?非常感谢
    • 我不确定我是否理解这个问题。
    • 我要加载该图片时如何调用它?
    • string[] filesInTheRoot = store.GetFileNames();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多