0

I know how to make a snapshot in WPF.

draggableObject = new Image();

AssociatedObjectParent.Children.Add(draggableObject);

Grid panel = AssociatedObject as Grid;

draggableObject.Height = panel.ActualHeight;
draggableObject.Width = panel.ActualWidth;

DrawingVisual dv = new DrawingVisual();

using (DrawingContext dc = dv.RenderOpen())
{
    VisualBrush vb = new VisualBrush(AssociatedObject);

    dc.DrawRectangle(vb, null, new Rect(new Point(0,0), new Size(draggableObject.Width, draggableObject.Height)));
}

double x = draggableObject.Width;
double y = draggableObject.Height;

RenderTargetBitmap renderTargetTd = new RenderTargetBitmap((int)x, (int)y, 96, 96, PixelFormats.Default);

RenderOptions.SetBitmapScalingMode(renderTargetTd, BitmapScalingMode.Fant);
RenderOptions.SetEdgeMode(renderTargetTd, EdgeMode.Aliased);

renderTargetTd.Render(dv);

But now I have this ScrollViewer that is 600px wide and Content with 1000px. When I use this code and my horizontalOffset is 0 (so I haven't scrolled at all), then all is well and I get an image of the ScrollViewer-Control just fine.

However when I scroll my ScrollView and make a snapshot just the same way. The snapshot is all messed up.

How can I make a snapshot of the visible '400 to 1000' area of the scrollview content like I can with the '0 to 600' area of the scrollview?

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • Did you try to render the Grid directly into the RenderTargetBitmap? Besides cleaner code, I think it will solve your problem because it suppose to render the Grid as it was layouted for display. – MaMazav Apr 03 '14 at 20:12
  • @MaMazav This doesn't work for me. I keep getting the part that is scrolled out of screen as well. I do get the right part of the ScrollViewer though. But if I scroll 100pxs. I get the right 500px of ScrollViewer and to the left of this a 100px area of nothingness. – Totumus Maximus Apr 04 '14 at 07:22
  • Well I think I found the cause of my troubles. On the ScrollViewer I have a block of about 150pxs that adjusts it's margin based on the HorizontalOffset of the ScrollViewer. This so I can make it appear that the block has a static spot on the screen while the content of the ScrollViewer slides underneath. When I remove the margin of this element, then it works fine. – Totumus Maximus Apr 04 '14 at 08:06
  • I'm happy to hear that it's work finally :) – MaMazav Apr 04 '14 at 12:32

0 Answers0