将字符串绘制到椭圆的中心

时间:2015-05-12 18:53:52

标签: c# drawing

我有简单的代码,可以绘制椭圆。我想在这个椭圆的中心画一个数字,我怎么能这样做?

绘制椭圆的代码是:

Point point = p.PointToClient(Cursor.Position);
float radius = 13.0f;
float x = point.X - radius;
float y = point.Y - radius;
float width = 2 * radius;
float height = 2 * radius;
graphicsObj.FillEllipse(myBrush, x, y, width, height);

2 个答案:

答案 0 :(得分:3)

使用Graphics.DrawString在周围矩形的中心绘制文字。指定一个StringFormat对象,其中Alignment和LineAlignment设置为StringAlignment.Center:

RectangleF bounds = new RectangleF(x, y, width, height);
using (StringFormat format = new StringFormat()) {
    format.Alignment = StringAlignment.Center;
    format.LineAlignment = StringAlignment.Center;
    graphicsObj.DrawText("Number", SystemFonts.Default, Brushes.Black, bounds, format);
}

答案 1 :(得分:0)

你可以使用:

来做到这一点
public void FillEllipse(
    Brush brush,
    Rectangle rect
)

public void DrawString(
    string s,
    Font font,
    Brush brush,
    RectangleF layoutRectangle
)

是:

graphicsObj.FillEllipse(brush,rect);
graphicsObj.DrawString("Number Here", new Font("Arial", 13f), Brushes.Black, sameRectangleOfEllipse);