private async void SaveMetaData(ProjectMetaData metaData, StorageFolder storageFolder) { //Create a text file to store the project metadata var file = await storageFolder.CreateFileAsync("metadata.txt", CreationCollisionOption.ReplaceExisting); var stream = await file.OpenAsync(FileAccessMode.ReadWrite); using (var outputStream = stream.GetOutputStreamAt(0)) { // Store information about the template chosen if it was enabled using (var dataWriter = new DataWriter(outputStream)) { if (metaData.templateVisibility == Visibility.Collapsed) { metaData.templateChoice = TemplateChoice.None; } dataWriter.WriteString($"{metaData.templateChoice.ToString()}"); await dataWriter.StoreAsync(); await outputStream.FlushAsync(); } } stream.Dispose(); }
public async Task <MainCanvasParams> OpenProject(List <InkStrokeContainer> currentStrokes, StorageFolder currentFolder, ProjectMetaData metaData, List <CanvasComponent> components) { List <InkStrokeContainer> newStrokes = new List <InkStrokeContainer>(); TemplateChoice templateChoice = TemplateChoice.None; //Let the user pick a project folder to open FolderPicker folderPicker = new FolderPicker(); folderPicker.FileTypeFilter.Add("*"); StorageFolder newFolder = await folderPicker.PickSingleFolderAsync(); if (newFolder != null) { IReadOnlyList <StorageFile> files = await newFolder.GetFilesAsync(); foreach (var f in files) { if (f.Name.Equals("metadata.txt")) { string text = await FileIO.ReadTextAsync(f); templateChoice = (TemplateChoice)Enum.Parse(typeof(TemplateChoice), text); } else if (f.Name.Equals("components.txt")) { // read file load shapes string text = await FileIO.ReadTextAsync(f); string[] xmlComponents = text.Split('\n'); foreach (string component in xmlComponents) { if (component.Length > 0) { components.Add(Serializer.Deserialize <CanvasComponent>(component)); } } } else if (f != null && f.FileType.Equals(".gif")) { // Open a file stream for reading. IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read); // Read from file. using (var inputStream = stream.GetInputStreamAt(0)) { var container = new InkStrokeContainer(); await container.LoadAsync(inputStream); //Add the strokes stored in the files newStrokes.Add(container); } stream.Dispose(); } } var result = await ConfirmSave(currentStrokes, currentFolder, metaData, components); if (result != ContentDialogResult.None) { return(new MainCanvasParams(newStrokes, newFolder, templateChoice, components)); } } return(null); }
public async Task <ContentDialogResult> ConfirmSave(List <InkStrokeContainer> strokes, StorageFolder folder, ProjectMetaData metaData, List <CanvasComponent> components) { //Open the dialog for confirming save before navigation away from the curent project TernaryButtonDialog t = new TernaryButtonDialog(); await t.ShowAsync(); if (t.result == ContentDialogResult.Primary) { //If they confirm save, execute save method await SaveProject(folder, strokes, metaData, components); } return(t.result); }
public async void SaveAsImage(double width, double height, List <InkStrokeContainer> _strokes, ProjectMetaData metaData, Canvas recognitionCanvas) { IsTooLarge = false; //Create a bitmap of the recognition canvas so it can be merged with the inkcanvas image WriteableBitmap recogWBitmap1 = await SaveRecognitionCanvas1(recognitionCanvas, width, height); //Create a second bitmap if the pixel stream is too large WriteableBitmap recogWBitmap; if (IsTooLarge) { recogWBitmap = await SaveRecognitionCanvas2(recognitionCanvas, width, height); } else { recogWBitmap = new WriteableBitmap((int)width, (int)height); } //Create an image to save the InkCanvas strokes to StorageFolder pictureFolder = ApplicationData.Current.LocalFolder; var file = await pictureFolder.CreateFileAsync("tempImage.png", CreationCollisionOption.ReplaceExisting); if (file != null) { //Write to the provided save file tempImage.png CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)width, (int)height, 96); //Add strokes to be drawn to the drawing session using (var ds = renderTarget.CreateDrawingSession()) { ds.Clear(Colors.White); foreach (var item in _strokes) { ds.DrawInk(item.GetStrokes()); } } using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { //Save the image to tempImage.png await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Png, 1f); } //Load tempImage.png as a BitmapImage then convert to WriteableBitmap BitmapImage bitmapImage = new BitmapImage(); WriteableBitmap w; var savedfiles = await pictureFolder.GetFilesAsync(); foreach (var imageFile in savedfiles) { if (imageFile.Name.StartsWith("tempImage")) { //Read the image file using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read)) { bitmapImage.DecodePixelHeight = 100; bitmapImage.DecodePixelWidth = 100; //Write the image into a bitmap await bitmapImage.SetSourceAsync(fileStream); } int h = bitmapImage.PixelHeight; int wd = bitmapImage.PixelWidth; using (var stream = await file.OpenReadAsync()) { //Obtain the bitmap dimensions for the WritatbleBitmap w = new WriteableBitmap(wd, h); //Write the contents of tempImage.png to the WritableBitmap await w.SetSourceAsync(stream); //Merge the inkcanvas image with the shapes on the recognition canvas SaveImageFile(await MergeImages(w, metaData, recogWBitmap1, recogWBitmap)); } } } } }
public async Task <StorageFolder> SaveProject(StorageFolder storageFolder, List <InkStrokeContainer> strokes, ProjectMetaData metaData, List <CanvasComponent> components) { //Use existing folder if (storageFolder != null) { //Save the strokes drawn, the project metadata so the current state of the eapplication is saved, and the shapes SaveStrokes(storageFolder, strokes); SaveMetaData(metaData, storageFolder); SaveCanvasComponents(components, storageFolder); return(storageFolder); } //Create new folder to save files SaveDialog save = new SaveDialog(); await save.ShowAsync(); if (save.result == ContentDialogResult.Primary) { //Store the chosen folder so it is automatically saved to the location specified in the future storageFolder = save.folder; SaveStrokes(storageFolder, strokes); SaveMetaData(metaData, storageFolder); SaveCanvasComponents(components, storageFolder); return(storageFolder); } return(null); }
private async Task <WriteableBitmap> MergeImages(WriteableBitmap originalImage, ProjectMetaData metaData, WriteableBitmap shapes1, WriteableBitmap shapes) { // Merge inkcanvas with recognition canvas var writeableBmp = new WriteableBitmap(1, 1); var mergedImage = originalImage; var background = metaData.bgImage; // background.Blit(new Rect(0, 0, background.PixelWidth, background.PixelHeight), mergedImage, new Rect(0, 0, mergedImage.PixelWidth, mergedImage.PixelHeight)); mergedImage.Blit(new Rect(0, 0, mergedImage.PixelWidth, mergedImage.PixelHeight), shapes1, new Rect(0, 0, shapes1.PixelWidth, shapes1.PixelHeight)); mergedImage.Blit(new Rect(0, 0, mergedImage.PixelWidth, mergedImage.PixelHeight), shapes, new Rect(0, 0, shapes.PixelWidth, shapes.PixelHeight)); //Merge image with template if visible if (metaData.templateVisibility == Visibility.Visible) { var templateImage = await writeableBmp.FromContent(new Uri($"ms-appx:///Assets/{metaData.templateChoice.ToString()}.png")); mergedImage.Blit(new Rect(0, 0, mergedImage.PixelWidth, mergedImage.PixelHeight), templateImage, new Rect(0, 0, templateImage.PixelWidth, templateImage.PixelHeight)); } //mergedImage.Blit(new Rect(0, 0, mergedImage.PixelWidth, mergedImage.PixelHeight), background, new Rect(0, 0, background.PixelWidth, background.PixelHeight)); return(mergedImage); }