private async Task SaveStrokesToFile(List <InkStroke> inkStrokes, StorageFolder folder, string filename) { StorageFile inkFile; if (inkStrokes.Count > 0) { InkStrokeContainer container = new InkStrokeContainer(); foreach (InkStroke stroke in inkStrokes) { container.AddStroke(stroke.Clone()); } inkFile = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream inkStream = await inkFile.OpenAsync(FileAccessMode.ReadWrite)) { await container.SaveAsync(inkStream); } AppEventSource.Log.Debug("InkingInApp: Inking saved to " + filename); } else { // Delete the file if there is no ink strokes. inkFile = await folder.TryGetItemAsync(filename) as StorageFile; if (inkFile != null) { await inkFile.DeleteAsync(); AppEventSource.Log.Debug("InkingInApp: Inking file removed. " + filename); } } }
public async void WriteXml(XmlWriter writer) { writer.WriteElementString("Title", Title); writer.WriteElementString("Description", Description); writer.WriteElementString("Status", Status.ToString()); writer.WriteStartElement("Stokes"); if (Strokes != null && Strokes.Count > 0) { using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { InkStrokeContainer inkCont = new InkStrokeContainer(); foreach (InkStroke stroke in Strokes) { inkCont.AddStroke(stroke.Clone()); } await inkCont.SaveAsync(ms); await ms.FlushAsync(); byte[] bytes = new byte[ms.Size]; //var dataWriter = new DataWriter(ms); var reader = new DataReader(ms.GetInputStreamAt(0)); await reader.LoadAsync((uint)ms.Size); reader.ReadBytes(bytes); writer.WriteBinHex(bytes, 0, (int)ms.Size); } } writer.WriteEndElement(); }
public void Redo() { foreach (var strokeId in StrokeIds) { var stroke = StrokeManager.CloneAndUpdateStroke(strokeId); InkStrokeContainer.AddStroke(stroke); } }
/// <summary> /// Save the model (family and their notes) to the app's isolated storage /// </summary> /// <remarks> /// The data format for notes data: /// /// Serialized model data (the people and the sticky notes) /// For each sticky note /// { /// int32 number of inkstrokes for the note /// } /// All ink stroke data (for all notes) combined into one container /// </remarks> /// <returns></returns> public async Task SaveModelAsync() { // Persist the model StorageFile notesDataFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(NOTES_MODEL_FILE, CreationCollisionOption.ReplaceExisting); using (Stream notesDataStream = await notesDataFile.OpenStreamForWriteAsync()) { // Serialize the model which contains the people and the stickyNote collection DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model)); serializer.WriteObject(notesDataStream, Model); } /* For each sticky note, save the number of inkstrokes it contains. * The function on the InkStrokeContainer that persists its contents is not designed * to save persist containers to the one stream. We also don't want to manage one * backing file per note. So combine the ink strokes into one container and persist that. * We'll seperate out the ink strokes to the right ink control by keeping track of how * many ink strokes belongs to each note */ InkStrokeContainer CombinedStrokes = new InkStrokeContainer(); StorageFile inkFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(NOTES_INK_FILE, CreationCollisionOption.ReplaceExisting); using (var randomAccessStream = await inkFile.OpenAsync(FileAccessMode.ReadWrite)) { using (IOutputStream inkStream = randomAccessStream.GetOutputStreamAt(0)) // DataWriter requires an IOutputStream { bool combinedStrokesHasContent = false; // whether we had any ink to save across all of the notes DataWriter writer = new DataWriter(inkStream); foreach (StickyNote Note in Model.StickyNotes) { // Save # strokes for this note if (Note.Ink != null && Note.Ink.GetStrokes().Count > 0) { IReadOnlyList <InkStroke> InkStrokesInNote = Note.Ink.GetStrokes(); writer.WriteInt32(InkStrokesInNote.Count); // capture the ink strokes into the combined container which will be saved at the end of the notes data file foreach (InkStroke s in InkStrokesInNote) { CombinedStrokes.AddStroke(s.Clone()); } combinedStrokesHasContent = true; } else { writer.WriteInt32(0); // not all notes have ink } } await writer.StoreAsync(); // flush the data in the writer to the inkStream // Persist the ink data if (combinedStrokesHasContent) { await CombinedStrokes.SaveAsync(inkStream); } } } }
private async Task SaveStrokesToBitmap(WriteableBitmap b) { Rect imgRect = new Rect(0, 0, b.PixelWidth, b.PixelHeight); InkStrokeContainer container = TheInkCanvas.InkPresenter.StrokeContainer; InkStrokeBuilder builder = new InkStrokeBuilder(); // Unsichtbare Tinte! InkDrawingAttributes da = TheInkCanvas.InkPresenter.CopyDefaultDrawingAttributes(); da.Size = new Size(0.1, 0.1); builder.SetDefaultDrawingAttributes(da); // Strich in oberer linker Ecke einfügen InkStroke topLeft = builder.CreateStroke(new List <Point>() { new Point(1, 1), new Point(2, 2) }); container.AddStroke(topLeft); // Strich in unterer Rechter Ecke einfügen InkStroke bottomRight = builder.CreateStroke(new List <Point>() { new Point(imgRect.Width - 2, imgRect.Height - 2), new Point(imgRect.Width - 1, imgRect.Height - 1) }); container.AddStroke(bottomRight); // Striche in WriteableBitmap speichern WriteableBitmap bmp; using (InMemoryRandomAccessStream ims = new InMemoryRandomAccessStream()) { await container.SaveAsync(ims); bmp = await new WriteableBitmap(1, 1) .FromStream(ims, BitmapPixelFormat.Bgra8); } // Bilder zusammenfügen b.Blit(imgRect, bmp, imgRect, WriteableBitmapExtensions.BlendMode.Alpha); }
public InkStroke AddStroke(InkStroke stroke) { var newStroke = stroke.Clone(); _strokeContainer.AddStroke(newStroke); AddStrokeEvent?.Invoke(this, new AddStrokeEventArgs(newStroke, stroke)); return(newStroke); }
private void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args) { this.pendingDry = this.inkSynchronizer.BeginDry(); var container = new InkStrokeContainer(); foreach (var stroke in this.pendingDry) { container.AddStroke(stroke.Clone()); } this.strokes.Add(container); canvas.Invalidate(); }
private InkStrokeContainer GetCurrentStrokesView(DateTimeOffset time) { var inkStrokeContainer = new InkStrokeContainer(); // The purpose of this sample is to demonstrate the timestamp usage, // not the algorithm. (The time complexity of the code is O(N^2).) foreach (InkStroke stroke in strokesToReplay) { InkStroke s = GetPartialStroke(stroke, time); if (s != null) { inkStrokeContainer.AddStroke(s); } } return(inkStrokeContainer); }
public async void WriteXml(XmlWriter writer) { writer.WriteElementString("Title", Title); writer.WriteElementString("Description", Description); writer.WriteElementString("Status", Status.ToString()); writer.WriteStartElement("Stokes"); if (Strokes != null && Strokes.Count > 0) { using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { InkStrokeContainer inkCont = new InkStrokeContainer(); foreach (InkStroke stroke in Strokes) { inkCont.AddStroke(stroke.Clone()); } await inkCont.SaveAsync(ms); await ms.FlushAsync(); byte[] bytes = new byte[ms.Size]; //var dataWriter = new DataWriter(ms); var reader = new DataReader(ms.GetInputStreamAt(0)); await reader.LoadAsync((uint)ms.Size); reader.ReadBytes(bytes); writer.WriteBinHex(bytes, 0, (int)ms.Size); } } else { byte[] bytes = new byte[1]; bytes[0] = 0xF; writer.WriteBinHex(bytes, 0, 1); } writer.WriteEndElement(); writer.WriteStartElement("Photo"); if (Photo != null) { InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream(); if (null != ms) { try { byte[] buffer = new byte[4 * Photo.PixelWidth * Photo.PixelHeight]; Photo.CopyToBuffer(buffer.AsBuffer()); writer.WriteBinHex(buffer, 0, (int)buffer.Length); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } } else { byte[] bytes = new byte[1]; bytes[0] = 0; writer.WriteBinHex(bytes, 0, 1); } writer.WriteEndElement(); }