I must print a displayed TreeView.
Rendering the root TreeViewItem to bitmap, gives me an image of the whole (even non visible nodes) tree. Then I split the bitmap in “pages” to be printed. The rendering code:
m_Bitmap = new RenderTargetBitmap((int)l_RootTreeViewItem.ActualHeightDesiredSize.Width,
(int)l_RootTreeViewItem.ActualHeight, 96, 96,
PixelFormats.Pbgra32);
m_Bitmap.Render(l_RootTreeViewItem);
Works well for small size trees. If the tree is large, RenderTargetBitmap results in “Out Of Memory” Exception.
So, the idea is to render only parts of the visual to avoid memory problems. A Render method where I can choose which part of visual to render will be perfect…
m_Bitmap.Render(l_RootTreeViewItem, xOffset, yOffset, width, height);
… but doesn’t exist. Is there some way to do that ?
What I’ll do :
VisualBrushof yourl_RootTreeViewItemRectangleand assign the visual brush to theFillpropertyVisualBrush.ViewboxandVisualBrush.Viewportto render the part of the tree view I’m interested inRenderTargetBitmap.Renderon my rectangle when neededEDIT
Solution 2
l_RootTreeViewItemin a canvasClipToBoundsproperty of the canvas to trueCanvas.Width,Canvas.Heightproperties andCanvas.Left,Canvas.Topattached properties to display only a part of theTreeViewItemUse
PrintDialog.PrintVisualon the canvas as needed.