1
votes

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 ?

1

1 Answers

1
votes

What I'll do :

  • Create a VisualBrush of your l_RootTreeViewItem
  • Create a Rectangle and assign the visual brush to the Fill property
  • Play with VisualBrush.Viewbox and VisualBrush.Viewport to render the part of the tree view I'm interested in
  • Use RenderTargetBitmap.Render on my rectangle when needed

EDIT

Solution 2

  • Put l_RootTreeViewItem in a canvas
  • Set the ClipToBounds property of the canvas to true
  • Play with Canvas.Width, Canvas.Height properties and Canvas.Left, Canvas.Top attached properties to display only a part of the TreeViewItem
  • Use PrintDialog.PrintVisual on the canvas as needed.

    <Canvas Width="300" Height="300" ClipToBounds="True">
        <TreeViewItem Canvas.Left="-200" Canvas.Top="-100">
            ...
        </TreeViewItem>
    </Canvas>