Come disegnare un rettangolo arrotondato con bordo a larghezza variabile all'interno di limiti specifici

StackOverflow https://stackoverflow.com/questions/628261

  •  06-07-2019
  •  | 
  •  

Domanda

Ho un metodo che disegna un rettangolo arrotondato con un bordo. Il bordo può essere di qualsiasi larghezza, quindi il problema che sto riscontrando è che si estende oltre i limiti indicati quando è spesso perché è disegnato dal centro di un percorso.

Come dovrei includere la larghezza del bordo in modo che si adatti perfettamente ai limiti indicati?

Ecco il codice che sto usando per disegnare il rettangolo arrotondato.

private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor)
{
    GraphicsPath gfxPath = new GraphicsPath();

    DrawPen.EndCap = DrawPen.StartCap = LineCap.Round;

    gfxPath.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
    gfxPath.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
    gfxPath.CloseAllFigures();

    gfx.FillPath(new SolidBrush(FillColor), gfxPath);
    gfx.DrawPath(DrawPen, gfxPath);
}
È stato utile?

Soluzione

Bene ragazzi, l'ho capito! Ho solo bisogno di ridurre i limiti per tenere conto della larghezza della penna. In un certo senso sapevo che questa era la risposta che mi chiedevo solo se ci fosse un modo per tracciare una linea all'interno di un percorso. Funziona bene però.

private void DrawRoundedRectangle(Graphics gfx, Rectangle Bounds, int CornerRadius, Pen DrawPen, Color FillColor)
{
    int strokeOffset = Convert.ToInt32(Math.Ceiling(DrawPen.Width));
    Bounds = Rectangle.Inflate(Bounds, -strokeOffset, -strokeOffset);

    DrawPen.EndCap = DrawPen.StartCap = LineCap.Round;

    GraphicsPath gfxPath = new GraphicsPath();
    gfxPath.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
    gfxPath.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
    gfxPath.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
    gfxPath.CloseAllFigures();

    gfx.FillPath(new SolidBrush(FillColor), gfxPath);
    gfx.DrawPath(DrawPen, gfxPath);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top