0

I'm saving strokes to a database and I can retrieve them. Now I want to save the color, width and transparency of the strokes as well.

This is the what I have in my code

private void AddFloorPlan()
{
     MyCustomStrokes customStrokes = new MyCustomStrokes();
     customStrokes.StrokeCollection = new Point[FloorPlanStrokes.Count][];
     for (int i = 0; i < FloorPlanStrokes.Count; i++)
     {
         customStrokes.StrokeCollection[i] =
         new Point[FloorPlanStrokes[i].StylusPoints.Count];
         for (int j = 0; j < FloorPlanStrokes[i].StylusPoints.Count; j++)
         {
             customStrokes.StrokeCollection[i][j] = new Point();
             customStrokes.StrokeCollection[i][j].X = FloorPlanStrokes[i].StylusPoints[j].X;
             customStrokes.StrokeCollection[i][j].Y = FloorPlanStrokes[i].StylusPoints[j].Y;
         }
     }
     MemoryStream ms = new MemoryStream();
     BinaryFormatter bf = new BinaryFormatter();
     bf.Serialize(ms, customStrokes);

     byte[] bytes = ms.ToArray();
     ms.Dispose();
}

[Serializable]
public sealed class MyCustomStrokes
{
    public MyCustomStrokes() { }
       /// <SUMMARY>
       /// The first index is for the stroke no.
       /// The second index is for the keep the 2D point of the Stroke.
       /// </SUMMARY>
    public Point[][] StrokeCollection;
}
Phil
  • 561
  • 2
  • 16
  • 29

1 Answers1

0

Store the color information as string (Color.ToString() do the job) and convert it to Color when deserialized as follows:

(Color)ColorConverter.ConvertFromString(STORED_STRING);

You would make new Brush and Freeze it if their color is not changed later.

Width (StrokeThickness?) is another thing. Just save it as double separately.

Example:

[Serializable]
public sealed class MyCustomStrokes
{
    public string[][] ColorCodeCollection;

    [NonSerialized()]
    private Dictionary<string, SolidColorBrush> _memo;
    public Brush GetBrush(int i, int j) 
    {
        var code = ColorCodeCollection[i][j];
        if(_memo.ContainsKey(code))
            return _memo[code];

        var col = (Color)ColorConverter.ConvertFromString(code);
        var brush = new SolidColorBrush(col);
        brush.Freeze();
        _memo[code] = brush;
        return brush;
    }
}

Note that this is just a quick code. Examine it meets your plan.

dytori
  • 487
  • 4
  • 12
  • Thanks for putting me in the right direction but I'm pretty new to C#. So how would put this now in the existing code? – Phil Mar 18 '16 at 08:30
  • Have edited the answer to show an example that only resume brushes, as I do not know how the colors are determined. No exception handling, and possible bad practices, but beyond the scope. – dytori Mar 19 '16 at 05:10