如何将图像从我的应用程序保存到WP8.1运行时应用程序中的图片库?任何有用的链接或代码段?
答案 0 :(得分:0)
包括第一个
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Graphics.Imaging;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
using System.IO;
然后宣布
BitmapImage bit = new BitmapImage();
string filename;
CameraCaptureTask cameraCaptureTask;
在构造函数
中 cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
然后复制粘贴
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show(e.ChosenPhoto.Length.ToString());
bit.SetSource(e.ChosenPhoto);
myImage.Source = bit;
filename = "WP_" + Convert.ToString(DateTime.Now.Ticks) + ".jpg";
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(filename))
isolatedStorage.DeleteFile(filename);
IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(filename);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bitmap);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
fileStream.Close();
}
//Code to display the photo on the page in an image control named myImage.
//System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
//bmp.SetSource(e.ChosenPhoto);
//myImage.Source = bmp;
}
}
然后把这个方法叫做你想要的地方
cameraCaptureTask.Show();
要添加到媒体库,请复制下面给出的代码
var library = new MediaLibrary();
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream fileStream = isolatedStorage.OpenFile(filename, FileMode.Open, FileAccess.ReadWrite);
var pic = library.SavePicture(filename, fileStream);
}