导入图片并保存到独立存储WP7 [英] Import picture and save to isolated storage WP7
本文介绍了导入图片并保存到独立存储WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在开发一个项目,用户需要从该项目中选择图片并导入。使用以下代码我可以导入图片,但我有几个问题。
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.
- 导入时命名的图片是什么? li>
- 导入后的图片位置
- 可以保存该图片,并在应用再次打开时重新加载(即使用独立存储) / li>
- What is the image named upon import?
- Where is the image located once imported
- Is it possible to save that image and reload it when the app is opened again (ie using isolated storage)
从教程中获取代码
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));
}
}
}
}
推荐答案
- PhotoResult包含OriginalFileName。
- 当PhotoChoserTask完成时PhotoResult.ChosenPhoto为您提供数据流
-
是的,您可以将图片存储在独立存储中。
- The PhotoResult contains OriginalFileName.
- When the PhotoChoserTask completes PhotoResult.ChosenPhoto gives you a stream to the data for the photo.
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();
}
}
这篇关于导入图片并保存到独立存储WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文