Windows Phone 8.1将StorageFile保存为图像

时间:2016-06-15 12:54:23

标签: c# image file windows-phone-8.1 save

我想将StorageFile保存为可以在图库中看到并稍后使用的图像。 StorageFile包含由相机拍摄的图像或从图库中获取并在StorageFile中转换的图像。

2 个答案:

答案 0 :(得分:0)

将包含已知图片扩展名(如.jpg或.png)的存储文件保存在公共文件夹中,它将显示在图库中。

答案 1 :(得分:0)

private async void SaveCroppedImage(object sender, RoutedEventArgs e)
       {
           StorageFile file = await KnownFolders.CameraRoll.CreateFileAsync("edited.jpg", CreationCollisionOption.ReplaceExisting);
           using (IRandomAccessStream stream =await file.OpenAsync(FileAccessMode.ReadWrite))
           {
               await EncodeWriteableBitmap(WB_CroppedImage, stream, BitmapEncoder.JpegEncoderId);
           }

       }
       private static async Task EncodeWriteableBitmap(WriteableBitmap bmp, IRandomAccessStream writeStream, Guid encoderId)
       {
           // Copy buffer to pixels
           byte[] pixels;
           using (var stream = bmp.PixelBuffer.AsStream())
           {
               pixels = new byte[(uint)stream.Length];
               await stream.ReadAsync(pixels, 0, pixels.Length);
           }
           // Encode pixels into stream
           var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
           encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
              (uint)bmp.PixelWidth, (uint)bmp.PixelHeight,
              96, 96, pixels);
           await encoder.FlushAsync();
       }