0

I'm working on an application that has an InkCanvas, an image that is set as the background image to the InkCanvas, as well as numerical data (one positive integer value per Stroke) that is tied into each Stroke on the InkCanvas.

My first mental approach was to overload the Stroke class to add the numerical data as a field. However, I'm not certain that I can save the StrokeCollection using InkCanvas.Strokes.Save(FileStream);

My second mental approach was to Save the collection of Strokes, and have a different collection of the numerical values for each Stroke stored in a separate SQLite file. However, I'm not certain that Strokes in the StrokeCollection are stored in order, and loaded back in order.

So the question is, would either of these two approaches work in order to store the Strokes, the numerical values associated with each Stroke, and the filename of the image or if possible the image itself, without generating more than one file?

Sakamoto Kazuma
  • 2,573
  • 7
  • 34
  • 75

1 Answers1

1

If for instance you wanted to save the strokes into an xml file, you can read/write the StrokeCollection like this:

 /// <summary>
/// Serializable version of StrokeCollection allowing for base64 saving using XMLSerializer
/// </summary>
[Serializable]
public class StrokeCollectionEx : StrokeCollection, IXmlSerializable
{
    public StrokeCollectionEx() { }

    #region " ReadXml "
    /// <summary>
    /// ReadXml
    /// </summary>
    /// <param name="reader"></param>
    public void ReadXml(XmlReader reader)
    {
        string s = reader.ReadElementContentAsString();
        byte[] strokeBits = Convert.FromBase64String(s);
        if (strokeBits != null && strokeBits.Length > 0)
        {
            using (MemoryStream ms = new MemoryStream(strokeBits))
            {
                //Reload this from stream
                this.Clear();
                StrokeCollection sc = new StrokeCollection(ms);
                foreach (Stroke x in sc)
                {
                    this.Add(x);
                }
            }
        }
    }
    #endregion

    #region " WriteXml "
    /// <summary>
    /// WriteXml
    /// </summary>
    /// <param name="writer"></param>
    public void WriteXml(XmlWriter writer)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Save(ms);
            byte[] strokeBits = ms.ToArray();

            writer.WriteBase64(strokeBits, 0, strokeBits.Length);
        }
    }
    #endregion

    #region " GetSchema "
    /// <summary>
    /// GetSchema
    /// </summary>
    /// <returns></returns>
    public XmlSchema GetSchema()
    {
        return null;
    }
    #endregion
}

You can probably cut out what you need to use to read/save to a database

Kevin Cook
  • 1,922
  • 1
  • 15
  • 16