0

I would like to export a grid (whit all his children) to a PNG. The problem is that some of these children are outside of the grid. Here is my code:

VisualBrush sourceBrush = new VisualBrush(MyGrid);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
    drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(Math.Floor(exportWidth), Math.Floor(exportHeight))));
    drawingContext.Close();
}

RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)Math.Floor(exportWidth), (int)Math.Floor(exportHeight), 96, 96, PixelFormats.Default);
renderTarget.Render(drawingVisual);

The resulting image is blurred if at least one of the children is outside the grid.

The exportHeight and exportWidth values are calculated upstream, relative to the position of the grid's children. If all children are inside the grid, the picture is clear. I think this is because of the VisualBrush original size that cannot be changed. Do you know a way to fix it ?

EDIT : I do not call renderTarget.Render(MyGrid); because it does not take in charge children who are outside the grid (Children whose top or left value is negative).

Ben
  • 3,972
  • 8
  • 43
  • 82
  • Sorry, but what is the VisualBrush and DrawVisual stuff good for? Why not just call `renderTarget.Render(MyGrid);`? – Clemens Jun 18 '13 at 19:33
  • Because it does not take into account children who are outside the grid (Children whose top or left value is negative). – Ben Jun 19 '13 at 07:43

1 Answers1

0

Have you tried?

MyGrid.ClipToBounds = true;
Ali
  • 83
  • 3
  • 10
  • Hi Ali. Since I've posted my question in 2013, I cannot remember what it was about. Maybe I will try to have a look at this old project to find out how I've solved it. – Ben Feb 28 '18 at 22:02
  • Hi @Ben I know. I just answered it because this happened to me recently and fixed my issue. So I answered as it may help other devs – Ali Mar 01 '18 at 23:01