InkCanvas (ink panel) usage in WPF
Zhou yinhui
ThanksWhen a BarberThis Cool Demo is provided for us.
You can download to it http://files.cnblogs.com/zhouyinhui/InkCanvas.rar here
The following are common usage of InkCanvas (assume that our InkCanvas object is inkCanv ):
Save
You can choose to save ink as an ink file or as an image
Save as an ink file (*. isf): this. inkCanv. Strokes. Save (file );
Where file is the file stream to be saved (FileStream)
Save as image:
We can first draw ink on the image and then save the image. To draw the Visual object to the image, we can use the RenderTargetBitmap class and use its Render () method. to Save an image, you can use the BitmapEncoder class and call its Save () method. renderTargetBitmap rtb = new RenderTargetBitmap (width, height, 0, 0, PixelFormats. default );
Rtb. Render (this. inkCanv );
BMP bitmapencoder encoder = new BMP bitmapencoder ();
Encoder. Frames. Add (BitmapFrame. Create (rtb ));
Encoder. Save (file );
Open
To open a saved ink file (*. isf), use the following method: this. inkCanv. Strokes = new StrokeCollection (file );
Selected
Like a canvas, you can select certain elements to be drawn. You can select the elements by using the mouse or by using the code.
Select with the mouse
You only need to switch the editing mode of InkCanvas. The EditingMode attribute of InkCanvas is of the InkCanvasEditingMode Enumeration type. You can switch the value to InkCanvasEditingMode. Select to enter the selected mode.
Select using code
You can call the Select () method of InkCanvas.
Copy, cut, paste, or delete a selected object
These operations are relatively simple. InkCanvas has corresponding methods: CopySelection (), CutSelection (), Past (), and Remove ().
Eraser
To use an eraser, The EditingMode attribute of InkCanvas is also switched. There are two erasure modes: InkCanvasEditingMode. eraseByPoint (point-by-point erasure) and InkCanvasEditingMode. eraseByStroke (erased by ink strokes, one uninterrupted stroke ).
Handwriting color, thickness...
To change the handwriting attributes, you can set the DefaultDrawingAttributes attribute of InkCanvas, for example, DrawingAttributes inkd = new DrawingAttributes ();
Inkd. Width = 5;
Inkd. Height = 5;
Inkd. Color = Color. FormArgb (255,255,255,255 );
This. inkCanv. DefaultDrawingAttributes = inkd;
InkCanvas also has many interesting functions. You can refer to the InkCanvas class in the SDK for more information.