3

I'm trying to take a screenshot of a datagrid which has to many rows to be displayed. So there is a scrollviewer. So when I just put the datagrid into the Render Method of RenderTargetBitmap I obviously just get the viewable part of the datagrid. I read that one can take a screenshot of a content when actually rendering the ItemsPresenter of the ScrollViewer of that control, as the ItemsPresenter would have the "real" Width and Height of the content.

Unfortunatly my ScrollViewer doesnt have any different Height, ActualHeight or RenderSize.Height than the dataGrid. So I always just get the visible part of the Content.

Anyone know how to do this the right way, that it actually takes the whole content ?

Code:

        var scroll = GetTemplateChildByName(dataGridInOut);
        if (scroll != null)
        {
            var item = scroll.Content as ItemsPresenter;
            var width = item.RenderSize.Width;
            var height = item.RenderSize.Height;
            var rtb = new RenderTargetBitmap((int) Math.Round(width), (int)Math.Round(height), 96, 96,
                                             PixelFormats.Pbgra32);
            var drawingVisual = new DrawingVisual();
            var visualBrush = new VisualBrush(item);
            using (var context = drawingVisual.RenderOpen())
            {
                context.DrawRectangle(visualBrush, null, new Rect(new Point(0,0), new Size(width, height)));
            }
            rtb.Render(drawingVisual);
            Clipboard.SetImage(rtb);
        }
david
  • 41
  • 4
  • The DataGrid control only renders what is visible, as an optimisation, so there is nothing more to see - you can see this for yourself using something like [WPF Inspector](http://wpfinspector.codeplex.com/) to view the visual tree of the DataGrid. You would need to convince the DataGrid that it has enough space, before using it as a visual. – Leaf Garland Mar 28 '12 at 16:03

1 Answers1

0

Leaf is right. You could instantiate another DataGrid bound to the same source programmatically, put it into a container which gives it infinite space, wait for it to render and then take a screenshot of this. No need to actually show it in the UI.

Zak
  • 724
  • 4
  • 18
  • I did this, but then the grids in the datagrid are rendered weird. Some of them are not in line anymore and change on every row. So I guess its not realy possible what I tried to do... :( – david Jun 12 '12 at 13:55
  • did you see this btw? http://stackoverflow.com/questions/1877115/create-wpf-element-offscreen-and-render-to-bitmap?rq=1 – Zak Jun 13 '12 at 11:21