Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I have an InkCanvas where strokes are collected.

StrokeCollection selectedStrokes = ic.GetSelectedStrokes();

to get the selected strokes and the bounds of just those selected strokes.

what is the best method to capture just the visual of those strokes --not the entire canvas--and their bounding rect so as to directly convert them into a bitmap?

Any help or suggestions are appreciated.

The closest I can find is:

Matrix m =

But this does not give me just the rectangle surrounding the strokes; infact, nothing showed up at all. (It failed).

I am in WPF C# .net 4.0

Edit: I'm trying to avoid using the strokes as an UI element, and stay strictly with the visual.

I also tried:
RectangleGeometry myRectangleGeometry = new RectangleGeometry();
myRectangleGeometry.Rect = inkBounds;

InkCanvas.Clip = myRectangleGeometry;

Which also fails.

Even doing RenderTargetBitmap.Render(InkCanvas) fails!

The articles mentioned only seem to work for UI elements -- is there a way to use strictly what is visually painted on the screen? (I am totally lost at this point).
Updated 26-May-14 21:51pm
Sergey Alexandrovich Kryukov 27-May-14 0:51am    
"Capture"?! Don't you see that you already have the Visual? :-)
You only need to render it on a bitmap to get a raster image of it.
awaynemd 27-May-14 1:12am    
I only want a small part of the canvas to be rendered as a bitmap?
awaynemd 27-May-14 1:15am    
Sorry...need to add, I would like to avoid using UI element and stay strictly with the visual. (Unless I have no choice).
awaynemd 27-May-14 3:39am    
bmp.Render(canvas) did not work.

You don't really need to "capture" the Visual, because System.Windows.Controls.InkCanvas is already System.Windows.Media.Visual:[^] :-).

If you need the whole canvas area, it's apparently canvas.ActualWidth and canvas.ActualHeight; and the bitmap could be obtained from any UI element using the class System.Windows.Media.Imaging.RenderTargetBitmap designed exactly for this purpose:[^].

In the page referenced above you can see some sample. Instead of drawingVisual shown in the code sample, render your canvas: bmp.Render(canvas).

Share this answer
Got it! The trick is to have the InkCanvas draw its strokes onto a new visual. Then use the new visual as the source to the RenderTargetBitmap:

Rect recBounds = inkBounds;

DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
dc.PushTransform(new TranslateTransform(-recBounds.X, -recBounds.Y));

dContext.DrawRectangle(Brushes.Transparent, null, recBounds);

RenderTargetBitmap rtb =
new RenderTargetBitmap((int)recBounds.Width,
96, 96, PixelFormats.Default);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900