Im using C# .net framework and WPF to create an Mspaint like application. I want to have a function that allows the user to save what they are working on and come back to it later. I've stored the canvas background and canvas strokes into a byte array to store in a DB, and I have found how to retrieve and convert the background image byte array, but I cant seem to figure out how to convert the InkStroke byte array data back to its original form.
I'll include examples for the code I was able to write
private StrokeCollection DisplayCanvasStrokeBytes(byte[] array)
{
//THIS is the method I am struggling to write
}
private byte[] GetInkByteArray()
{
MemoryStream ms = new MemoryStream();
DrawingCanvas.Strokes.Save(ms);
byte[] bytes = ms.ToArray();
return bytes;
}
private byte[] GetBitMapOfCanvasBackground()
{
//strokes are cleared so that ink can be edited in the future
DrawingCanvas.Strokes.Clear();
MemoryStream ms = new MemoryStream();
//this is the same code used to "save as new image", so essentially
//I saved a new image of just the background and used that to save to the db
//this is not the best practice and is a bit over-complicated, but I was struggling to figure out
//How to save DrawingCanvas.Background as a byte array
int marg = int.Parse(DrawingCanvas.Margin.Left.ToString());
RenderTargetBitmap rtb =
new RenderTargetBitmap((int)DrawingCanvas.ActualWidth - marg,
(int)DrawingCanvas.ActualHeight - marg, 0, 0,
PixelFormats.Default);
rtb.Render(DrawingCanvas);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(ms);
byte[] bytes = ms.ToArray();
return bytes;
}
private ImageBrush DisplayBackgroundImageBytes(byte[] array)
{
ImageBrush brush;
BitmapImage bi;
using (var ms = new MemoryStream(array))
{
brush = new ImageBrush();
bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.None;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = ms;
bi.EndInit();
}
brush.ImageSource = bi;
return brush;
}