Import picture and save to isolated storage WP7

筅森魡賤 提交于 2020-01-04 07:00:51

问题


I am currently working on a project where the user needs to select a image from there photo gallery and import it. Using the following code I am able to import the picture but I have a few questions.

  1. What is the image named upon import?
  2. Where is the image located once imported
  3. Is it possible to save that image and reload it when the app is opened again (ie using isolated storage)

Heres the code from a tutorial

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:


  1. The PhotoResult contains OriginalFileName.
  2. When the PhotoChoserTask completes PhotoResult.ChosenPhoto gives you a stream to the data for the photo.
  3. Yes at that point you can store the image in isolated storage.

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


来源:https://stackoverflow.com/questions/7615095/import-picture-and-save-to-isolated-storage-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!