質問

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.

役に立ちましたか?

解決

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, ...);
    ....
}

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top