diff --git a/ANAConversationSimulator/Helpers/Utils.cs b/ANAConversationSimulator/Helpers/Utils.cs index 68c4372..ec5fb79 100644 --- a/ANAConversationSimulator/Helpers/Utils.cs +++ b/ANAConversationSimulator/Helpers/Utils.cs @@ -4,16 +4,22 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.Foundation.Collections; +using Windows.Graphics.Imaging; using Windows.Storage; +using Windows.Storage.Streams; using Windows.System.Profile; using Windows.UI.Popups; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Media.Imaging; namespace ANAConversationSimulator.Helpers { @@ -218,6 +224,28 @@ public static T DeepCopy(this T source) return JsonConvert.DeserializeObject(json); } + public static string NormalizeFileName(string text) + { + if (text == null) + text = "New File"; + foreach (var c in Path.GetInvalidFileNameChars()) + text.Replace(c, '_'); + return text; + } + + public static async Task SaveVisualElementToFile(FrameworkElement element, StorageFile file) + { + var renderTargetBitmap = new RenderTargetBitmap(); + await renderTargetBitmap.RenderAsync(element, (int)element.Width, (int)element.Height); + var pixels = await renderTargetBitmap.GetPixelsAsync(); + using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) + { + var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); + byte[] bytes = pixels.ToArray(); + encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, 96, 96, bytes); + await encoder.FlushAsync(); + } + } } public enum DeviceFormFactorType diff --git a/ANAConversationSimulator/Views/MainPage.xaml b/ANAConversationSimulator/Views/MainPage.xaml index e0a9539..7f63890 100644 --- a/ANAConversationSimulator/Views/MainPage.xaml +++ b/ANAConversationSimulator/Views/MainPage.xaml @@ -23,11 +23,13 @@ - - + + - + + + diff --git a/ANAConversationSimulator/Views/MainPage.xaml.cs b/ANAConversationSimulator/Views/MainPage.xaml.cs index 4a09cc9..590e488 100644 --- a/ANAConversationSimulator/Views/MainPage.xaml.cs +++ b/ANAConversationSimulator/Views/MainPage.xaml.cs @@ -96,32 +96,4 @@ private async Task SaveChatAsImage(string fileName = null) } } } -} - -/* - private async void SaveChatAsImage() - { - RenderTargetBitmap renderTarget = new RenderTargetBitmap(); - await renderTarget.RenderAsync(ThreadScrollViewer, (int)ThreadScrollViewer.ScrollableWidth, (int)ThreadScrollViewer.ScrollableHeight); - - IBuffer pixelBuffer = await renderTarget.GetPixelsAsync(); - - InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); - var buffer = await renderTarget.GetPixelsAsync(); - - WriteableBitmap img = new WriteableBitmap(renderTarget.PixelWidth, renderTarget.PixelHeight); - var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); - encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)renderTarget.PixelWidth, (uint)renderTarget.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, buffer.ToArray()); - await encoder.FlushAsync(); - - await img.SetSourceAsync(stream); - - //SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)ThreadScrollViewer.ScrollableWidth, (int)ThreadScrollViewer.ScrollableHeight, BitmapAlphaMode.Premultiplied); - - StorageFolder sFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("ANA Chat Flows", CreationCollisionOption.OpenIfExists); - var sFile = await sFolder.CreateFileAsync((DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss") + ".png"), CreationCollisionOption.GenerateUniqueName); - using (var fileStream = await sFile.OpenStreamForWriteAsync()) - using (var s = img.PixelBuffer.AsStream()) - s.CopyTo(fileStream); - } - */ +} \ No newline at end of file