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

Draw String to centre of ellipse

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

绘制椭圆的代码是:

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

使用 Graphics.DrawString to draw text in center of surrounding rectangle. Specify an 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);
}

您可以使用:

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