InkCanvas Load/Save operations

lexeme picture lexeme · Jun 5, 2011 · Viewed 9.4k times · Source

I've never used InkCanvas control before. What I need is to load up a file into InkCanvas, draw some scribbles and get ther resulting image. And I want to make some additional operations with gotten image.

As for saving

Correct me if I'm wrong. I've found a link: http://www.centrolutions.com/Blog/post/2008/12/09/Convert-WPF-InkCanvas-to-Bitmap.aspx According to the post will be loaded image considered in addition to user scribbles. Or it only converts scribbles to bitmap?

As for loading

How do I load image using OpenFileDialog? I don't want to use ISF.

Thanks!

Answer

keyboardP picture keyboardP · Jun 5, 2011

Saving:

If you want to be able to manipulate strokes after saving, then you need to save the strokes. You can do this by using the StrokeCollection.Save method.

var fs = new FileStream(inkFileName, FileMode.Create);
inkCanvas1.Strokes.Save(fs);

You can then load this again and have the individual strokes accessible. However, once you render it out (e.g. to a bitmap) then that rendered file can only be loaded as a Bitmap and not individual strokes. (Of course, you can do both and save the strokes as a separate file). To save as a bitmap, you can use the code in the link you posted to.

Loading

Loading a bitmap to an Image control is straightforward since the OpenFileDialog will return the image path.

if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
{
    myImageControl.Source = new BitmapImage(new Uri(myOpenFileDialog.FileName, UriKind.Absolute));
}

That will load the image and display it in an image control on your form.

Edit: I don't think you can load a bitmap straight to an InkCanvas. However, you can load the strokes instead.

To load the strokes again, you can use StrokeCollection(Stream)

var fs = new FileStream(inkFileName,
                FileMode.Open, FileAccess.Read);
StrokeCollection strokes = new StrokeCollection(fs);
inkCanvas1.Strokes = strokes;

For more functions, you can read this CodeProject article.