4

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!

lexeme
  • 2,915
  • 10
  • 60
  • 125

1 Answers1

5

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.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • 1) Will code from the post above store both user's scribbles and background image as one image? – lexeme Jun 05 '11 at 21:57
  • 2) What do you mean when writing `MyImageControl`. I need to upload image into `InkCanvas`. I've found no such a property? – lexeme Jun 05 '11 at 21:58
  • 1) No. The users scribbles (`Strokes`) are stored in a separate file. 2) I don't think InkCanvas can load bitmaps. Instead, you can load the strokes again using StrokeCollection(Stream) http://msdn.microsoft.com/en-us/library/ms522846.aspx (I've updated the answer) – keyboardP Jun 05 '11 at 22:07
  • But I read it is quite overkill reading/writing strokes. And I need to get pixels from resulting image. It means I need not separate result but combined(modified image). – lexeme Jun 05 '11 at 22:13
  • 2
    If you want to analyse pixels, then `InkCanvas` is not the best control to use to do that. Once you've created the image in InkCanvas, you can save a bitmap (from the link you gave) and convert the byte array to a `Bitmap` control. (http://msdn.microsoft.com/en-us/library/dwdz9wf7.aspx) To convert: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/e57f7731-c703-4c17-b1a2-32b155f9b745/ You can then use the Bitmap.GetPixel method to get the pixels or, if you need efficiency, use this method: http://www.bobpowell.net/lockingbits.htm – keyboardP Jun 05 '11 at 22:20
  • I know how to analyse. I don't know how to upload image into `InkCanvas`. I decided to use this kind of control cause it really reduces painting handling. – lexeme Jun 05 '11 at 22:27