public async Task <SoftwareBitmap> SaveToBitmap(InkStrokeContainer ink) { ICanvasResourceCreator device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, Width, Height, 96); var src = new byte[Width * 4 * Height]; for (int index = 0; index < src.Length; ++index) { src[index] = 255; } renderTarget.SetPixelBytes(src); byte[] imageBytes = new byte[4 * Width * Height]; using (var ds = renderTarget.CreateDrawingSession()) { IReadOnlyList <InkStroke> inklist = ink.GetStrokes(); ds.DrawInk(inklist); } var inkpixel = renderTarget.GetPixelBytes(); WriteableBitmap bmp = new WriteableBitmap(Width, Height); Stream s = bmp.PixelBuffer.AsStream(); s.Seek(0, SeekOrigin.Begin); s.Write(inkpixel, 0, Width * 4 * Height); s.Position = 0; await s.ReadAsync(imageBytes, 0, (int)s.Length); SoftwareBitmap result = new SoftwareBitmap(BitmapPixelFormat.Bgra8, Width, Height, BitmapAlphaMode.Premultiplied); result.CopyFromBuffer(imageBytes.AsBuffer()); return(result); }
public async Task <SoftwareBitmap> SaveToBitmap(InkStrokeContainer ink) { ICanvasResourceCreator device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, Image.PixelWidth, Image.PixelHeight, 96); renderTarget.SetPixelBytes(new byte[Image.PixelWidth * 4 * Image.PixelHeight]); byte[] imageBytes = new byte[4 * Image.PixelWidth * Image.PixelHeight]; using (var ds = renderTarget.CreateDrawingSession()) { Image.CopyToBuffer(imageBytes.AsBuffer()); var win2dRenderedBitmap = CanvasBitmap.CreateFromBytes( device, imageBytes, Image.PixelWidth, Image.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 96.0f); ds.DrawImage(win2dRenderedBitmap); IReadOnlyList <InkStroke> inklist = ink.GetStrokes(); ds.DrawInk(inklist); } var inkpixel = renderTarget.GetPixelBytes(); WriteableBitmap bmp = new WriteableBitmap((int)Image.PixelWidth, (int)Image.PixelHeight); Stream s = bmp.PixelBuffer.AsStream(); s.Seek(0, SeekOrigin.Begin); s.Write(inkpixel, 0, (int)Image.PixelWidth * 4 * (int)Image.PixelHeight); s.Position = 0; await s.ReadAsync(imageBytes, 0, (int)s.Length); Image.CopyFromBuffer(imageBytes.AsBuffer()); return(Image); }
private static async void AddStrokeCountToRecords(List <InkSample> records) { foreach (InkSample record in records) { string hexInput = record.ISF; try { InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0))) { writer.WriteBytes(HexStringToByteArray(hexInput)); await writer.StoreAsync(); var iSC = new InkStrokeContainer(); await iSC.LoadAsync(stream); record.StrokeCount = iSC.GetStrokes().Count; } stream.Dispose(); } catch (Exception ex) { Console.WriteLine("Error parsing ISF!"); } } }
private async void BtnSave_Click(object sender, RoutedEventArgs e) { InkStrokeContainer container = new InkStrokeContainer(); foreach (var item in _inkStrokes) { container.AddStrokes(from stroke in item.GetStrokes() select stroke.Clone()); } if (container.GetStrokes().Count > 0) { Windows.Storage.Pickers.FileSavePicker savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; savePicker.FileTypeChoices.Add("GIF with embedded ISF", new List <string>() { ".gif" }); savePicker.DefaultFileExtension = ".gif"; savePicker.SuggestedFileName = "PaperPencilPen001"; Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { Windows.Storage.CachedFileManager.DeferUpdates(file); IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); using (IOutputStream outputStream = stream.GetOutputStreamAt(0)) { await container.SaveAsync(outputStream); await outputStream.FlushAsync(); } stream.Dispose(); Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { //file saved } else { //not saved } } else { //cancelled } } }
private async void saveGIF(InkStrokeContainer strokeContainer) { var strokes = strokeContainer.GetStrokes(); if (strokes.Count > 0) { Windows.Storage.Pickers.FileSavePicker savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; savePicker.FileTypeChoices.Add( "Obrazek", new List <string>() { ".gif" }); savePicker.DefaultFileExtension = ".gif"; savePicker.SuggestedFileName = "Nowy gif"; Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); // When chosen, picker returns a reference to the selected file. if (file != null) { Windows.Storage.CachedFileManager.DeferUpdates(file); IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); using (IOutputStream outputStream = stream.GetOutputStreamAt(0)) { await strokeContainer.SaveAsync(outputStream); await outputStream.FlushAsync(); } stream.Dispose(); Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { Debug.WriteLine("File " + file.Name + " was saved."); } else { Debug.WriteLine("File " + file.Name + " was NOT saved."); } } else { Debug.WriteLine("Cancelled"); } } }
/// <summary> /// Loads inking from file in the app data folder. /// </summary> /// <param name="pageNumber"></param> /// <returns></returns> public async Task <InkStrokeContainer> LoadInking(int pageNumber) { InkStrokeContainer inkStrokeContainer = new InkStrokeContainer(); if (await InkingFolder.TryGetItemAsync(InkFileName(pageNumber)) is StorageFile inkFile) { inkStrokeContainer = await LoadInkFile(inkFile, pageNumber); inkStrokesDict[pageNumber] = new List <InkStroke>(inkStrokeContainer.GetStrokes()); } return(inkStrokeContainer); }
public PointStrokeCollection Convert(InkStrokeContainer strokes, double simplification, bool hq) { var pointStrokes = new PointStrokeCollection(); foreach (InkStroke stroke in strokes.GetStrokes()) { var inkPoints = stroke.GetInkPoints(); var points = simplify(inkPoints, simplification, hq); pointStrokes.Add(points); } return(pointStrokes); }
public async Task <List <InkStroke> > LoadErasedStrokes(int pageNumber) { if (!erasedStrokesDict.TryGetValue(pageNumber, out List <InkStroke> erasedStrokes)) { erasedStrokes = new List <InkStroke>(); InkStrokeContainer inkStrokeContainer = new InkStrokeContainer(); if (await ErasedFolder.TryGetItemAsync(ErasedInkFileName(pageNumber)) is StorageFile inkFile) { inkStrokeContainer = await LoadInkFile(inkFile, pageNumber); erasedStrokes.AddRange(inkStrokeContainer.GetStrokes()); erasedStrokesDict[pageNumber] = erasedStrokes; } } return(erasedStrokes); }
public async void ReadXml(XmlReader reader) { reader.Read(); Title = reader.ReadElementContentAsString("Title", ""); Description = reader.ReadElementContentAsString("Description", ""); string s = reader.ReadElementContentAsString("Status", ""); Status = (JobStatus)Enum.Parse(typeof(JobStatus), s); InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream(); DataWriter dw = new DataWriter(ms.GetOutputStreamAt(0)); byte[] tempBytes = new byte[1024]; int bytesRead = 0; do { bytesRead = reader.ReadElementContentAsBinHex(tempBytes, 0, 1024); if (bytesRead > 0) { dw.WriteBytes(tempBytes); } } while (bytesRead == 1024); await dw.StoreAsync(); if (ms.Size > 0) { InkStrokeContainer inkCont = new InkStrokeContainer(); await inkCont.LoadAsync(ms); Strokes = inkCont.GetStrokes().ToList(); } reader.Skip(); }
public async void ReadXml(XmlReader reader) { reader.Read(); Title = reader.ReadElementContentAsString("Title", ""); Description = reader.ReadElementContentAsString("Description", ""); string s = reader.ReadElementContentAsString("Status", ""); Status = (JobStatus)Enum.Parse(typeof(JobStatus), s); if (!reader.IsEmptyElement) { InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream(); DataWriter dw = new DataWriter(ms.GetOutputStreamAt(0)); byte[] tempBytes; int bytesRead = 0; int totalBytes = 0; do { tempBytes = new byte[1024]; bytesRead = reader.ReadElementContentAsBinHex(tempBytes, 0, 1024); if (bytesRead > 0) { dw.WriteBytes(tempBytes); totalBytes += bytesRead; } } while (bytesRead == 1024); await dw.StoreAsync(); if (totalBytes > 1) { InkStrokeContainer inkCont = new InkStrokeContainer(); await inkCont.LoadAsync(ms); Strokes = inkCont.GetStrokes().ToList(); } reader.ReadEndElement(); } else { reader.Read(); } if (!reader.IsEmptyElement) { InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream(); DataWriter dw = new DataWriter(ms.GetOutputStreamAt(0)); byte[] tempBytes; int bytesRead = 0; int totalBytesRead = 0; do { tempBytes = new byte[1024]; bytesRead = reader.ReadContentAsBinHex(tempBytes, 0, 1024); if (bytesRead > 0) { dw.WriteBytes(tempBytes); totalBytesRead += bytesRead; } } while (bytesRead == 1024); await dw.StoreAsync(); if (totalBytesRead > 1) { //load bytes as image byte[] bytes = new byte[ms.Size]; //var dataWriter = new DataWriter(ms); var dataReader = new DataReader(ms.GetInputStreamAt(0)); await dataReader.LoadAsync((uint)ms.Size); dataReader.ReadBytes(bytes); //TODO: this should change based on the resolution you store the photos at Photo = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 640, 360); Photo.CopyFromBuffer(bytes.AsBuffer()); } reader.ReadEndElement(); } else { reader.Read(); } reader.Skip(); }
/// <summary> /// Load the family and their notes from local storage /// </summary> /// <returns>Null if there was no model to load, otherwise, the deserialized model</returns> private async Task <Model> LoadModelAsync() { Model model = null; InkStrokeContainer combinedStrokes = new InkStrokeContainer(); // To avoid managing individual files for every InkCanvas, we will combine all ink stroke information into one container List <int> InkStrokesPerCanvas = new List <int>(); try { StorageFile modelDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_MODEL_FILE); using (IRandomAccessStream randomAccessStream = await modelDataFile.OpenAsync(FileAccessMode.ReadWrite)) { // Load the model which contains the people and the note collection try { DataContractJsonSerializer modelSerializer = new DataContractJsonSerializer(typeof(Model)); model = (Model)modelSerializer.ReadObject(randomAccessStream.AsStream()); } catch (System.Runtime.Serialization.SerializationException) { System.Diagnostics.Debug.Assert(false, "Failed to load serialized model"); return(null); } } // For each sticky note, load the number of inkstrokes it contains StorageFile inkDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_INK_FILE); using (IInputStream inkStream = await inkDataFile.OpenSequentialReadAsync()) { bool combinedStrokesExist = false; DataReader reader = new DataReader(inkStream); foreach (StickyNote n in model.StickyNotes) { await reader.LoadAsync(sizeof(int)); // You need to buffer the data before you can read from a DataReader. int numberOfInkStrokes = reader.ReadInt32(); InkStrokesPerCanvas.Add(numberOfInkStrokes); combinedStrokesExist |= numberOfInkStrokes > 0; } // Load the ink data if (combinedStrokesExist) { await combinedStrokes.LoadAsync(inkStream); } } // using inkStream } // try catch (FileNotFoundException) { // No data to load. We'll start with a fresh model return(null); } // Factor out the inkstrokes from the big container into each note int allStrokesIndex = 0, noteIndex = 0; IReadOnlyList <InkStroke> allStrokes = combinedStrokes.GetStrokes(); foreach (StickyNote n in model.StickyNotes) { // InkStrokeContainers can't be serialized using the default xml/json serialization. // So create a new one and fill it up from the data we restored n.Ink = new InkStrokeContainer(); // pull out the ink strokes that belong to this note for (int i = 0; i < InkStrokesPerCanvas[noteIndex]; i++) { n.Ink.AddStroke(allStrokes[allStrokesIndex++].Clone()); } ++noteIndex; } return(model); }
public void save(InkStrokeContainer strokeContainer) { this.createCSV(strokeContainer.GetStrokes()); this.saveGIF(strokeContainer); }
/// <summary> /// Captures the ink from an <see cref="InkCanvas"/> control. /// </summary> /// <param name="strokeContainer"> /// The <see cref="InkStrokeContainer"/> containing the strokes to be rendered. /// </param> /// <param name="rootRenderElement"> /// A <see cref="FrameworkElement"/> which wraps the canvas. /// </param> /// <param name="targetFile"> /// A <see cref="StorageFile"/> to store the image in. /// </param> /// <param name="encoderId"> /// A <see cref="BitmapEncoder"/> ID to use to render the image. /// </param> /// <returns> /// Returns the <see cref="StorageFile"/> containing the rendered ink. /// </returns> public static async Task <StorageFile> CaptureInkToFileAsync( this InkStrokeContainer strokeContainer, FrameworkElement rootRenderElement, StorageFile targetFile, Guid encoderId) { if (targetFile != null) { var renderBitmap = new RenderTargetBitmap(); await renderBitmap.RenderAsync(rootRenderElement); var bitmapSizeAt96Dpi = new Size(renderBitmap.PixelWidth, renderBitmap.PixelHeight); var pixels = await renderBitmap.GetPixelsAsync(); var win2DDevice = CanvasDevice.GetSharedDevice(); using ( var target = new CanvasRenderTarget( win2DDevice, (float)rootRenderElement.ActualWidth, (float)rootRenderElement.ActualHeight, 96.0f)) { using (var drawingSession = target.CreateDrawingSession()) { using ( var canvasBitmap = CanvasBitmap.CreateFromBytes( win2DDevice, pixels, (int)bitmapSizeAt96Dpi.Width, (int)bitmapSizeAt96Dpi.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized, 96.0f)) { drawingSession.DrawImage( canvasBitmap, new Rect(0, 0, target.SizeInPixels.Width, target.SizeInPixels.Height), new Rect(0, 0, bitmapSizeAt96Dpi.Width, bitmapSizeAt96Dpi.Height)); } drawingSession.Units = CanvasUnits.Pixels; drawingSession.DrawInk(strokeContainer.GetStrokes()); } using (var stream = await targetFile.OpenAsync(FileAccessMode.ReadWrite)) { var displayInfo = DisplayInformation.GetForCurrentView(); var encoder = await BitmapEncoder.CreateAsync(encoderId, stream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, target.SizeInPixels.Width, target.SizeInPixels.Height, displayInfo.RawDpiX, displayInfo.RawDpiY, target.GetPixelBytes()); await encoder.FlushAsync(); } } } return(targetFile); }
public IEnumerable <InkStroke> GetStrokes() => _strokeContainer.GetStrokes();