InkCanvas Load/Save operations

2019-04-10 17:34发布

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!

1条回答
三岁会撩人
2楼-- · 2019-04-10 18:17

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.

查看更多
登录 后发表回答