Take the 2-minute tour ×

In order to handle multiple resolutions I have following controls in the same xaml page.

  • Grid -> Viewbox (stretch, centered) -> Canvas (centered) -> Rectangle X (centered)
  • FrameworkElement

I am trying to cover the whole page with a black overlay and cut a rectangle for the Rectangle X in the center of the page.

To create the black overlay I paint the whole page black and then :

        DrawingVisual dv = new DrawingVisual();

        using (DrawingContext dc = dv.RenderOpen())
            double sw = SystemParameters.PrimaryScreenWidth;
            double sh = SystemParameters.PrimaryScreenHeight;
            PathGeometry cmbg = null;

            RectangleGeometry rectangleFullScreen = new RectangleGeometry(new Rect(0, 0, sw, sh));
            cmbg = Geometry.Combine(Geometry.Empty, rectangleFullScreen, GeometryCombineMode.Union, null);

            RectangleGeometry rectangleHole = new RectangleGeometry(holeRectangle);
            cmbg = Geometry.Combine(cmbg, rectangleHole, GeometryCombineMode.Exclude, null);

            Brush b = new SolidColorBrush(Colors.Black);
            b.Opacity = 0.85;
            dc.DrawGeometry(b, new Pen(), cmbg);

        return dv;

In order to calculate the rectangle to exclude I get the screen coordinates of the RectangleX (.PointToScreen(new Point(0, 0));)

And the problem is that if I modify the size of the Viewbox (depending on the Resolution) the RectangleX changes its location but the PointToScreen always returns the same Screen coordinate.

Witout changing the viewbox the hole is cut at the right place, just above the RectangleX.

How can I get the correct coordinate of the RectangleX in order to draw the exclude rectangle?

I would be very grateful for your help.

Thank You


share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I have been struggling with this for couple of days now...but found the solution 10 minutes after posting the question...


After changing the width and max width of the Viewbox I have to call UpdateLayout()

It works now!


share|improve this answer
add comment

Your Answer


