2009-03-09 40 views
17

Tengo un método que dibuja un rectángulo redondeado con un borde. El borde puede tener cualquier ancho, por lo que el problema que tengo es que el borde se extiende más allá de los límites dados cuando es grueso porque se dibuja desde el centro de una ruta.Cómo dibujar un rectángulo redondeado con un borde de ancho variable dentro de límites específicos

¿Cómo incluiría el ancho del borde para que se ajuste perfectamente en los límites dados?

Aquí está el código que estoy usando para dibujar el rectángulo redondeado.

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

Respuesta

29

¡Muy bien chicos, lo descubrí! Solo necesita reducir los límites para tener en cuenta el ancho del bolígrafo. Sabía que esta era la respuesta que me preguntaba si había una manera de trazar una línea en el interior de un camino. Esto funciona bien sin embargo.

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); 
} 
Cuestiones relacionadas