Question

I am working with WPF DrawingVisual and Pen and encountered a problem.

When I draw a DrawingVisual with Pen, say, a Rectangle as follows:

Pen StrokePen = new Pen();
StrokePen.Brush = Brushes.SkyBlue;
StrokePen.Thickness = 6;

DrawingVisual dv = new DrawingVisual
DrawingContext dc = dv.RenderOpen();
dc.DrawingRectangle(......., StrokePen, ......);
dc.Close();

I found that the half of the Stroke cover the rectangle like the following:

enter image description here

Therefore, if the Thickness of the Pen is too large so that it even larger than the Rectangle, the Rectangle will disappear (The whole rectangle is covered by the Stroke).

Could I adjust some setting so that the Stroke (Pen) drawn on the rectangle will not cover the rectangle (only draw beyond the sides of the rectangle)

Thank you.

Was it helpful?

Solution

You could simply draw the Rectangle twice, first with a Pen, then with a Brush:

using (DrawingContext dc = dv.RenderOpen())
{
    ...
    dc.DrawingRectangle(null, StrokePen, ...); 
    dc.DrawingRectangle(FillBrush, null, ...);
    ....
}

OTHER TIPS

In wpf border of rectangle is its internal content so no there is no way to force it to be outside of rectangle. But you can adjust size of your rectangle to compensate for Pen.Thickness.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top