Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task SaveImage()
- {
- var grid = (GetView() as ShellView).FindName("img") as Grid;
- RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
- await renderTargetBitmap.RenderAsync(grid, StepInfo.CroppedImage.Width, StepInfo.CroppedImage.Height);
- IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
- WriteableBitmap wb = new WriteableBitmap(renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
- pixels.CopyTo(wb.PixelBuffer);
- //Tutaj trzeba zdobyć obiekt StorageFile
- wb.Save(storageFileInstance) //defaultowo nie ma funkcji Save, wiec podaje kod do tego
- }
- public static async Task Save(this WriteableBitmap writeableBitmap, StorageFile outputFile, Guid encoderId)
- {
- Stream stream = writeableBitmap.PixelBuffer.AsStream();
- byte[] pixels = new byte[(uint)stream.Length];
- await stream.ReadAsync(pixels, 0, pixels.Length);
- using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
- {
- var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
- encoder.SetPixelData(
- BitmapPixelFormat.Bgra8,
- BitmapAlphaMode.Premultiplied,
- (uint)writeableBitmap.PixelWidth,
- (uint)writeableBitmap.PixelHeight,
- 96, // TODO: Shouldnt we get this from the image?
- 96,
- pixels);
- await encoder.FlushAsync();
- using (var outputStream = writeStream.GetOutputStreamAt(0))
- {
- await outputStream.FlushAsync();
- }
- }
- }
- public static async Task Save(this WriteableBitmap writeableBitmap, StorageFile outputFile)
- {
- Guid encoderId;
- var ext = "." + outputFile.FileType.ToLower();
- if (new[] { ".bmp", ".dib" }.Contains(ext))
- {
- encoderId = BitmapEncoder.BmpEncoderId;
- }
- else if (new[] { ".tiff", ".tif" }.Contains(ext))
- {
- encoderId = BitmapEncoder.TiffEncoderId;
- }
- else if (new[] { ".gif" }.Contains(ext))
- {
- encoderId = BitmapEncoder.GifEncoderId;
- }
- else if (new[] { ".jpg", ".jpeg", ".jpe", ".jfif", ".jif" }.Contains(ext))
- {
- encoderId = BitmapEncoder.JpegEncoderId;
- }
- else if (new[] { ".hdp", ".jxr", ".wdp" }.Contains(ext))
- {
- encoderId = BitmapEncoder.JpegXREncoderId;
- }
- else
- {
- encoderId = BitmapEncoder.PngEncoderId;
- }
- await writeableBitmap.Save(outputFile, encoderId);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement