如何在wp8.1中保存图片库中的图像

时间:2014-11-01 16:09:36

标签: windows-phone-8.1

如何将图像从我的应用程序保存到WP8.1运行时应用程序中的图片库?任何有用的链接或代码段?

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

    }