Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导入图片并保存到独立存储WP7_C#_Image_Windows Phone 7_Storage - Fatal编程技术网

C# 导入图片并保存到独立存储WP7

C# 导入图片并保存到独立存储WP7,c#,image,windows-phone-7,storage,C#,Image,Windows Phone 7,Storage,我目前正在进行一个项目,用户需要从照片库中选择一个图像并导入它。使用下面的代码,我可以导入图片,但我有几个问题 导入时图像的名称是什么 导入后,图像位于何处 是否可以保存该图像并在再次打开应用程序时重新加载(即使用独立存储) 这是教程中的代码 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.

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

  • 导入时图像的名称是什么
  • 导入后,图像位于何处
  • 是否可以保存该图像并在再次打开应用程序时重新加载(即使用独立存储)
  • 这是教程中的代码

    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));
    
                }
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    Net系统;
    使用System.Windows;
    使用System.Windows.Controls;
    使用System.Windows.Documents;
    使用System.Windows.Input;
    使用System.Windows.Media;
    使用System.Windows.Media.Animation;
    使用System.Windows.Shapes;
    使用Microsoft.Phone.Controls;
    使用Microsoft.Phone.Tasks;
    使用System.IO;
    使用System.Windows.Media.Imaging;
    名称空间PhoneApp4
    {
    公共部分类主页:PhoneApplicationPage
    {
    //建造师
    公共主页()
    {
    初始化组件();
    }
    PhotoChooserTask selectphoto=null;
    私有无效按钮1\u单击(对象发送者,路由目标)
    {
    选择Photo=新建PhotoChooserTask();
    selectphoto.Completed+=新事件处理程序(selectphoto\u Completed);
    选择photo.Show();
    }
    void selectphoto_已完成(对象发送方,PhotoResult e)
    {
    if(e.TaskResult==TaskResult.OK)
    {
    BinaryReader=新的BinaryReader(e.ChosenPhoto);
    image1.Source=新的位图图像(新的Uri(例如OriginalFileName));
    }
    }
    }
    }
    
  • PhotoResult包含原始文件名
  • 当PhotoHoserTask完成时,PhotoResult.ChosenPhoto将为您提供照片数据流
  • 是的,此时您可以将图像存储在独立存储器中

      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[]filesinterroot=store.GetFileNames();