Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
 public async Task WriteInkAsync(InkStrokeContainer strokeContainer)
 {
     using (Stream stream = await _folder.OpenStreamForWriteAsync(_name + InkFileExtension, CreationCollisionOption.ReplaceExisting))
     {
         await strokeContainer.SaveAsync(stream.AsOutputStream(), InkPersistenceFormat.Isf);
     }
 }
        /// <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);
                    }
                }
            }
        }
Exemplo n.º 5
0
        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");
                }
            }
        }
Exemplo n.º 7
0
        public async Task <FileUpdateStatus> SaveInkFileAsync(StorageFile file)
        {
            if (file != null)
            {
                // Prevent updates to the file until updates are finalized with call to CompleteUpdatesAsync.
                CachedFileManager.DeferUpdates(file);

                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await _strokeContainer.SaveAsync(stream);
                }

                // Finalize write so other apps can update file.
                return(await CachedFileManager.CompleteUpdatesAsync(file));
            }

            return(FileUpdateStatus.Failed);
        }
        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);
        }
Exemplo n.º 9
0
        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();
        }