我是Windows Phone 7应用程序开发的新手。我正在使用PhotoChooserTask类访问图片库。从图片库中选择一张图片后,我想将图片库中的图片(.jpg文件)添加到我的应用程序的图片文件夹中。这该怎么做 ?我使用以下代码
public partial class MainPage : PhoneApplicationPage
{
PhotoChooserTask photoChooserTask;
// Constructor
public MainPage()
{
InitializeComponent();
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
}
}
}
我想将所选图像动态添加到我的应用程序的图像文件夹中。这该怎么做?能否请您提供我可以解决上述问题的任何代码或链接?
答案 0 :(得分:4)
以下是将所选图片保存到IsolatedStorage然后将其读出以在页面上显示的示例:
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
var contents = new byte[1024];
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var local = new IsolatedStorageFileStream("image.jpg", FileMode.Create, store))
{
int bytes;
while ((bytes = e.ChosenPhoto.Read(contents, 0, contents.Length)) > 0)
{
local.Write(contents, 0, bytes);
}
}
// Read the saved image back out
var fileStream = store.OpenFile("image.jpg", FileMode.Open, FileAccess.Read);
var imageAsBitmap = PictureDecoder.DecodeJpeg(fileStream);
// Display the read image in a control on the page called 'MyImage'
MyImage.Source = imageAsBitmap;
}
}
}
答案 1 :(得分:0)
您可以在此处参考修订后的Photo Chooser API上的RTM release notes或doco。
答案 2 :(得分:0)
实际上,一旦获得了流,就可以将其转换为byte并在本地存储。以下是Task_Completed事件处理程序中应该具有的内容:
using (MemoryStream stream = new MemoryStream())
{
byte[] contents = new byte[1024];
int bytes;
while ((bytes = e.ChosenPhoto.Read(contents, 0, contents.Length)) > 0)
{
stream.Write(contents, 0, bytes);
}
using (var local = new IsolatedStorageFileStream("myImage.jpg", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
{
local.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
}
}