将照片保存在媒体库窗口手机8.1中

时间:2015-09-01 21:01:44

标签: c# xaml windows-phone-8.1

我从列表视图中获取照片路径并在详细屏幕中显示图像。 详细页面代码在这里。

 //Imaged  is my image control in Xaml.

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var imagePath = e.Parameter as string;
        imaged.Source = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));


        var imgsource = imaged.Source;
    }

我想通过点击保存按钮事件将此图片保存在媒体库中。

这是我的保存按钮事件代码。

    private async void ApplicationBarIconButton_Click_SaveToPictures(object sender, RoutedEventArgs e)
    {

        FileSavePicker picker = new FileSavePicker();

           // picker.FileTypeChoices.Add("PNG File", new List<string>() { ".png" });
            picker.FileTypeChoices.Add("JPEG image", new string[] { ".jpg" });
            picker.FileTypeChoices.Add("PNG image", new string[] { ".png" });
            picker.FileTypeChoices.Add("BMP image", new string[] { ".bmp" });
            picker.DefaultFileExtension = ".png";



            picker.SuggestedFileName = "bomdiaimg";
            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

            StorageFile file = await picker.PickSaveFileAsync();
           // StorageFile file = await picker.PickSaveFileAndContinue();
            if (file != null) 
            { 
               RenderTargetBitmap renderTargetBitMap = new RenderTargetBitmap();  



            await  renderTargetBitMap.RenderAsync(imaged, (int)imaged.Width, (int)imaged.Height); 

            var pixels = await renderTargetBitMap.GetPixelsAsync();



            using (IRandomAccessStream randomAccessStream = await file.OpenAsync(FileAccessMode.ReadWrite))  
             { 
                  var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, randomAccessStream);  
                  byte[] bytes = pixels.ToArray();  
                  encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitMap.PixelWidth, (uint)renderTargetBitMap.PixelHeight, 96, 96, bytes); await encoder.FlushAsync();  
             }
            }

    }

我无法保存图片错误就是

此处错误是一个例外。

      StorageFile file = await picker.PickSaveFileAsync();

SOme在此方法下排列绿线&#34;表示使用picksave文件并继续&#34;但是当我使用这种方法然后错误显示。无法理解这个问题。如何在媒体库中保存我的图片

2 个答案:

答案 0 :(得分:0)

SaveJpeg方法是名称空间System.Windows.Media.Imaging.Extensions的扩展方法。在源文件的顶部添加一个使用:

using System.Windows.Media.Imaging.Extensions;

而且,您可能打算使用System.Windows.Media.Imaging.WriteableBitmap代替Windows.UI.Xaml.Media.Imaging.WriteableBitmap

答案 1 :(得分:0)

You maybe want use RenderTargetBitMap for save control image. Please check this example code.

FileSavePicker picker = new FileSavePicker(); 
picker.FileTypeChoices.Add("JPG File", new List<string>() { ".jpg" });  
 StorageFile file = await picker.PickSaveFileAsync(); if (file != null) 
{ 
   RenderTargetBitmap renderTargetBitMap = new RenderTargetBitmap();  
//With this method we can make our XAML elements in an image. await       


renderTargetBitMap.RenderAsync(grdRender, (int)grdRender.Width, (int)grdRender.Height); 

var pixels = await renderTargetBitMap.GetPixelsAsync();



using (IRandomAccessStream randomAccessStream = await file.OpenAsync(FileAccessMode.ReadWrite))  
 { 
      var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, randomAccessStream);  
      byte[] bytes = pixels.ToArray();  
      encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitMap.PixelWidth, (uint)renderTargetBitMap.PixelHeight, 96, 96, bytes); await encoder.FlushAsync();  
 }
}