Skip to content

Commit

Permalink
Merge branch 'private-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
NizamLZ committed Aug 18, 2017
2 parents 817fba0 + c707246 commit e815caa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
28 changes: 28 additions & 0 deletions ANAConversationSimulator/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -218,6 +224,28 @@ public static T DeepCopy<T>(this T source)
return JsonConvert.DeserializeObject<T>(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
Expand Down
8 changes: 5 additions & 3 deletions ANAConversationSimulator/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
</Grid>
</controls:PageHeader.Content>
<controls:PageHeader.SecondaryCommands>
<AppBarButton Click="{x:Bind ViewModel.Reset}" Label="Reset" />
<AppBarButton Click="{x:Bind ViewModel.UpdateAPI}" Label="Configure Chat Server" />
<AppBarButton Click="{x:Bind Reset}" Label="Reset" />
<AppBarButton Click="{x:Bind ViewModel.UpdateAPI}" Label="Update APIs" />
<AppBarButton Click="{x:Bind ViewModel.ShowMemoryStack}" Label="Show Memory Stack" />
<AppBarButton Click="{x:Bind ViewModel.AgentChat}" Label="Agent Chat" />

<AppBarSeparator />
<AppBarButton Click="{x:Bind SaveChatAsImageDefault}" Label="Save Snapshot" />
<AppBarButton Click="{x:Bind SaveChatAsImageWithName}" Label="Save Snapshot As.." />
</controls:PageHeader.SecondaryCommands>
</controls:PageHeader>
<Grid x:Name="ContentGrid" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" RelativePanel.Below="pageHeader" Background="White">
Expand Down
30 changes: 1 addition & 29 deletions ANAConversationSimulator/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,4 @@ private async Task<bool> 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);
}
*/
}

0 comments on commit e815caa

Please sign in to comment.