private static async Task GenerateBitmap(CancellationToken ct, string folderName, string fileName, FrameworkElement content) { var parent = content.Parent as FrameworkElement; if (parent != null) { parent.MinWidth = 400; parent.MinHeight = 400; } content.MinWidth = 400; content.MinHeight = 400; var border = content.FindFirstChild <Border>(); if (border != null) { border.Background = new SolidColorBrush(Colors.White); } Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap bmp = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); await bmp.RenderAsync(content.Parent as FrameworkElement).AsTask(ct); content.DataContext = null; var pixels = await bmp.GetPixelsAsync().AsTask(ct); var folder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync( folderName, Windows.Storage.CreationCollisionOption.OpenIfExists ).AsTask(ct); if (folder == null) { folder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName).AsTask(ct); } var file = await folder.CreateFileAsync( fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting ).AsTask(ct); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite).AsTask(ct)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream).AsTask(ct); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bmp.PixelWidth, (uint)bmp.PixelHeight, DisplayInformation.GetForCurrentView().RawDpiX, DisplayInformation.GetForCurrentView().RawDpiY, pixels.ToArray() ); await encoder.FlushAsync().AsTask(ct); } }
internal static string ImageToDataUri(Image img) { if (img == null) { Debug.Assert(false); return(string.Empty); } byte[] pb = null; #if KeePassUWP var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); bitmap.RenderAsync(img).GetResults(); var pixelBuffer = bitmap.GetPixelsAsync().GetResults(); pb = new byte[pixelBuffer.Length]; var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(pixelBuffer); dataReader.ReadBytes(pb); dataReader.Dispose(); #else using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Png); pb = ms.ToArray(); } #endif return(StrUtil.DataToDataUri(pb, "image/png")); }
async System.Threading.Tasks.Task<Windows.Storage.StorageFile> CaptureAndSave(UIElement element) { // capture var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); await bitmap.RenderAsync(element); var picker = new Windows.Storage.Pickers.FileSavePicker { SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary, FileTypeChoices = { { "JPEG", new[] { ".jpg" } } }, SuggestedFileName = "Image", DefaultFileExtension = ".jpg", CommitButtonText = "Save", }; // save var file = await picker.PickSaveFileAsync(); using (var stream = await file.OpenStreamForWriteAsync()) { var pixel = await bitmap.GetPixelsAsync(); var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi; var random = stream.AsRandomAccessStream(); var jpg = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId; var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(jpg, random); var format = Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8; var alpha = Windows.Graphics.Imaging.BitmapAlphaMode.Ignore; var width = (uint)bitmap.PixelWidth; var height = (uint)bitmap.PixelHeight; encoder.SetPixelData(format, alpha, width, height, dpi, dpi, pixel.ToArray()); await encoder.FlushAsync(); } return file; }
// Create SaveLabeledImageButton_Click to handle clicking saveLabeledImageButton. // Allow use of "byte[] bytes = pixels.ToArray();". //using System.Runtime.InteropServices.WindowsRuntime; private async void SaveLabeledImageButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap renderTargetBitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(this.ImageCanvas); Windows.Storage.Pickers.FileSavePicker pickerToSaveLabeledImage = new Windows.Storage.Pickers.FileSavePicker(); pickerToSaveLabeledImage.FileTypeChoices.Add("PNG Image", new string[] { ".png" }); pickerToSaveLabeledImage.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" }); Windows.Storage.StorageFile fileToWhichToSave = await pickerToSaveLabeledImage.PickSaveFileAsync(); if (fileToWhichToSave != null) { Windows.Storage.Streams.IBuffer pixels = await renderTargetBitmap.GetPixelsAsync(); using (Windows.Storage.Streams.IRandomAccessStream stream = await fileToWhichToSave.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)) { Windows.Graphics.Imaging.BitmapEncoder encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, stream); encoder.SetPixelData( Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi, pixels.ToArray()); await encoder.FlushAsync(); } } }
public static async System.Threading.Tasks.Task SavePathImageAsFile(int Width, int Height, string fileName, FrameworkElement element, bool UseRenderTarget = true) { double oldWidth = element.Width; double oldHeight = element.Height; double actOldWidth = element.ActualWidth; double actOldHeight = element.ActualHeight; //if (!UseRenderTarget) { //engine takes the Ceiling so make sure its below or sometimes off by 1 rounding up from ActualWidth/Height element.Width = !UseRenderTarget?Math.Floor((float)Math.Min(Window.Current.Bounds.Width, Width)) : (float)Math.Min(Window.Current.Bounds.Width, Width); element.Height = !UseRenderTarget?Math.Floor((float)Math.Min(Window.Current.Bounds.Height, Height)) : (float)Math.Min(Window.Current.Bounds.Height, Height); //bool bHasCalledUpdateLayout = false; //should wrap into another event handler and check a bHasCalledUpdateLayout to ignore early calls and race condition //object lockVar = new object(); //EventHandler<object> eventHandler = null; //System.Threading.Tasks.TaskCompletionSource<object> t = new System.Threading.Tasks.TaskCompletionSource<object>(); //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - element.Width) <= 1 && Math.Abs(element.ActualHeight - element.Height) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } }; //element.LayoutUpdated += eventHandler; //lock (lockVar) { // element.UpdateLayout(); // bHasCalledUpdateLayout = true; //} ////await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => element.Dispatcher.ProcessEvents(Windows.UI.Core.CoreProcessEventsOption.ProcessAllIfPresent))); //await t.Task; //element.LayoutUpdated -= eventHandler; await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { }); if (!UseRenderTarget && (element.ActualWidth > element.Width || element.ActualHeight > element.Height)) { if (element.ActualWidth > element.Width) { element.Width -= 1; } if (element.ActualHeight > element.Height) { element.Height -= 1; } //bHasCalledUpdateLayout = false; //t = new System.Threading.Tasks.TaskCompletionSource<object>(); //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - element.Width) <= 1 && Math.Abs(element.ActualHeight - element.Height) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } }; //element.LayoutUpdated += eventHandler; //lock (lockVar) //{ // element.UpdateLayout(); // bHasCalledUpdateLayout = true; //} //await t.Task; //element.LayoutUpdated -= eventHandler; await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { }); } } #if WINDOWS_APP && STORETOOLKIT if (!UseRenderTarget) { System.IO.MemoryStream memstream = await WinRTXamlToolkit.Composition.WriteableBitmapRenderExtensions.RenderToPngStream(element); Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName + ".png", Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); await stream.WriteAsync(memstream.GetWindowsRuntimeBuffer()); stream.Dispose(); } else { #endif //Canvas cvs = new Canvas(); //cvs.Width = Width; //cvs.Height = Height; //Windows.UI.Xaml.Shapes.Path path = new Windows.UI.Xaml.Shapes.Path(); //object val; //Resources.TryGetValue((object)"PathString", out val); //Binding b = new Binding //{ // Source = (string)val //}; //BindingOperations.SetBinding(path, Windows.UI.Xaml.Shapes.Path.DataProperty, b); //cvs.Children.Add(path); float dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi; Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap wb = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); await wb.RenderAsync(element, (int)((float)Width * 96 / dpi), (int)((float)Height * 96 / dpi)); Windows.Storage.Streams.IBuffer buf = await wb.GetPixelsAsync(); Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName + ".png", Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); //Windows.Graphics.Imaging.BitmapPropertySet propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); //propertySet.Add("ImageQuality", new Windows.Graphics.Imaging.BitmapTypedValue(1.0, Windows.Foundation.PropertyType.Single)); // Maximum quality Windows.Graphics.Imaging.BitmapEncoder be = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, stream); //, propertySet); be.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied, (uint)wb.PixelWidth, (uint)wb.PixelHeight, dpi, dpi, buf.ToArray()); await be.FlushAsync(); await stream.GetOutputStreamAt(0).FlushAsync(); stream.Dispose(); #if WINDOWS_APP && STORETOOLKIT } #endif //if (!UseRenderTarget) { element.Width = oldWidth; element.Height = oldHeight; //bHasCalledUpdateLayout = false; //t = new System.Threading.Tasks.TaskCompletionSource<object>(); //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - actOldWidth) <= 1 && Math.Abs(element.ActualHeight - actOldHeight) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } }; //element.LayoutUpdated += eventHandler; //lock (lockVar) //{ // element.UpdateLayout(); // bHasCalledUpdateLayout = true; //} //await t.Task; //element.LayoutUpdated -= eventHandler; await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { }); } }
public static async System.Threading.Tasks.Task SavePathImageAsFile(int Width, int Height, string fileName, FrameworkElement element, bool UseRenderTarget = true) { double oldWidth = element.Width; double oldHeight = element.Height; double actOldWidth = element.ActualWidth; double actOldHeight = element.ActualHeight; //if (!UseRenderTarget) { //engine takes the Ceiling so make sure its below or sometimes off by 1 rounding up from ActualWidth/Height element.Width = !UseRenderTarget ? Math.Floor((float)Math.Min(Window.Current.Bounds.Width, Width)) : (float)Math.Min(Window.Current.Bounds.Width, Width); element.Height = !UseRenderTarget ? Math.Floor((float)Math.Min(Window.Current.Bounds.Height, Height)) : (float)Math.Min(Window.Current.Bounds.Height, Height); //bool bHasCalledUpdateLayout = false; //should wrap into another event handler and check a bHasCalledUpdateLayout to ignore early calls and race condition //object lockVar = new object(); //EventHandler<object> eventHandler = null; //System.Threading.Tasks.TaskCompletionSource<object> t = new System.Threading.Tasks.TaskCompletionSource<object>(); //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - element.Width) <= 1 && Math.Abs(element.ActualHeight - element.Height) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } }; //element.LayoutUpdated += eventHandler; //lock (lockVar) { // element.UpdateLayout(); // bHasCalledUpdateLayout = true; //} ////await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => element.Dispatcher.ProcessEvents(Windows.UI.Core.CoreProcessEventsOption.ProcessAllIfPresent))); //await t.Task; //element.LayoutUpdated -= eventHandler; await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { }); if (!UseRenderTarget && (element.ActualWidth > element.Width || element.ActualHeight > element.Height)) { if (element.ActualWidth > element.Width) element.Width -= 1; if (element.ActualHeight > element.Height) element.Height -= 1; //bHasCalledUpdateLayout = false; //t = new System.Threading.Tasks.TaskCompletionSource<object>(); //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - element.Width) <= 1 && Math.Abs(element.ActualHeight - element.Height) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } }; //element.LayoutUpdated += eventHandler; //lock (lockVar) //{ // element.UpdateLayout(); // bHasCalledUpdateLayout = true; //} //await t.Task; //element.LayoutUpdated -= eventHandler; await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { }); } } #if WINDOWS_APP && STORETOOLKIT if (!UseRenderTarget) { System.IO.MemoryStream memstream = await WinRTXamlToolkit.Composition.WriteableBitmapRenderExtensions.RenderToPngStream(element); Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName + ".png", Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); await stream.WriteAsync(memstream.GetWindowsRuntimeBuffer()); stream.Dispose(); } else { #endif //Canvas cvs = new Canvas(); //cvs.Width = Width; //cvs.Height = Height; //Windows.UI.Xaml.Shapes.Path path = new Windows.UI.Xaml.Shapes.Path(); //object val; //Resources.TryGetValue((object)"PathString", out val); //Binding b = new Binding //{ // Source = (string)val //}; //BindingOperations.SetBinding(path, Windows.UI.Xaml.Shapes.Path.DataProperty, b); //cvs.Children.Add(path); float dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi; Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap wb = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); await wb.RenderAsync(element, (int)((float)Width * 96 / dpi), (int)((float)Height * 96 / dpi)); Windows.Storage.Streams.IBuffer buf = await wb.GetPixelsAsync(); Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName + ".png", Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); //Windows.Graphics.Imaging.BitmapPropertySet propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); //propertySet.Add("ImageQuality", new Windows.Graphics.Imaging.BitmapTypedValue(1.0, Windows.Foundation.PropertyType.Single)); // Maximum quality Windows.Graphics.Imaging.BitmapEncoder be = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, stream);//, propertySet); be.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied, (uint)wb.PixelWidth, (uint)wb.PixelHeight, dpi, dpi, buf.ToArray()); await be.FlushAsync(); await stream.GetOutputStreamAt(0).FlushAsync(); stream.Dispose(); #if WINDOWS_APP && STORETOOLKIT } #endif //if (!UseRenderTarget) { element.Width = oldWidth; element.Height = oldHeight; //bHasCalledUpdateLayout = false; //t = new System.Threading.Tasks.TaskCompletionSource<object>(); //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - actOldWidth) <= 1 && Math.Abs(element.ActualHeight - actOldHeight) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } }; //element.LayoutUpdated += eventHandler; //lock (lockVar) //{ // element.UpdateLayout(); // bHasCalledUpdateLayout = true; //} //await t.Task; //element.LayoutUpdated -= eventHandler; await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { }); } }