Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. public async Task SaveImage()
  2. {
  3.  
  4.             var grid = (GetView() as ShellView).FindName("img") as Grid;
  5.  
  6.             RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
  7.             await renderTargetBitmap.RenderAsync(grid, StepInfo.CroppedImage.Width, StepInfo.CroppedImage.Height);
  8.  
  9.             IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
  10.             WriteableBitmap wb = new WriteableBitmap(renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
  11.  
  12.             pixels.CopyTo(wb.PixelBuffer);
  13.  
  14.             //Tutaj trzeba zdobyć obiekt StorageFile
  15.  
  16.             wb.Save(storageFileInstance) //defaultowo nie ma funkcji Save, wiec podaje kod do tego
  17. }
  18.  
  19.  
  20.  public static async Task Save(this WriteableBitmap writeableBitmap, StorageFile outputFile, Guid encoderId)
  21.         {
  22.             Stream stream = writeableBitmap.PixelBuffer.AsStream();
  23.             byte[] pixels = new byte[(uint)stream.Length];
  24.             await stream.ReadAsync(pixels, 0, pixels.Length);
  25.  
  26.             using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
  27.             {
  28.                 var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
  29.                 encoder.SetPixelData(
  30.                     BitmapPixelFormat.Bgra8,
  31.                     BitmapAlphaMode.Premultiplied,
  32.                     (uint)writeableBitmap.PixelWidth,
  33.                     (uint)writeableBitmap.PixelHeight,
  34.                     96,                 // TODO: Shouldnt we get this from the image?
  35.                     96,
  36.                     pixels);
  37.                 await encoder.FlushAsync();
  38.  
  39.                 using (var outputStream = writeStream.GetOutputStreamAt(0))
  40.                 {
  41.                     await outputStream.FlushAsync();
  42.                 }
  43.             }
  44.         }
  45.  
  46.  
  47.         public static async Task Save(this WriteableBitmap writeableBitmap, StorageFile outputFile)
  48.         {
  49.             Guid encoderId;
  50.  
  51.             var ext = "." + outputFile.FileType.ToLower();
  52.  
  53.             if (new[] { ".bmp", ".dib" }.Contains(ext))
  54.             {
  55.                 encoderId = BitmapEncoder.BmpEncoderId;
  56.             }
  57.             else if (new[] { ".tiff", ".tif" }.Contains(ext))
  58.             {
  59.                 encoderId = BitmapEncoder.TiffEncoderId;
  60.             }
  61.             else if (new[] { ".gif" }.Contains(ext))
  62.             {
  63.                 encoderId = BitmapEncoder.GifEncoderId;
  64.             }
  65.             else if (new[] { ".jpg", ".jpeg", ".jpe", ".jfif", ".jif" }.Contains(ext))
  66.             {
  67.                 encoderId = BitmapEncoder.JpegEncoderId;
  68.             }
  69.             else if (new[] { ".hdp", ".jxr", ".wdp" }.Contains(ext))
  70.             {
  71.                 encoderId = BitmapEncoder.JpegXREncoderId;
  72.             }
  73.             else
  74.             {
  75.                 encoderId = BitmapEncoder.PngEncoderId;
  76.             }
  77.  
  78.             await writeableBitmap.Save(outputFile, encoderId);
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement