DrawingContext.DrawRectangle does not observe the position unless some other geometry is added RRS feed

  • Question

  • I use DrawingGroup context to draw geometries and then create a DrawingImage from it like so.

    var dg = new DrawingGroup();
    using (var dc = dg.Open())
        var rec = new Rect(new Point(100, 100), new Size(20, 20));
        dc.DrawRectangle(null, myPen, rec);
        //adding this line will make the rectangle drawn at the correct position
        //dc.DrawLine(myPen, new Point(0, 0), new Point(0, 1));
    var di = new DrawingImage(dg);
    Image = di;

    The problem is that the rectangle is drawn at 0,0 coordinates unless I add a line geometry as shown in the example code above. The size of the line geometry does not matter, but it should be greater than zero and its pen should not be null.

    Is this behaviour correct?

    Saturday, August 10, 2019 9:52 PM

All replies

  • Does adding dc.Close() before end of using block help?

    If so, maybe there are some race condition happen with delayed rendering.

    Monday, August 12, 2019 4:36 AM
  • Hi Yaugenka,

    Thank you for posting here.

    According to your code, I have a question to confirm with you.

    What's the type of your application? WPF? Winform? 

    Please provide more details, and it will help us analyze your problem.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 12, 2019 6:55 AM