6

テキストを中央に配置するために提案されたすべての方法を試しましたが、個々の文字を中央に配置する際に必要な結果が得られないようです.

私は長方形を持っています。その長方形に、DrawEllipse で円を描いています。今度は、同じ四角形と DrawString を使用して円の内側に 1 つの文字を描画し、完全に中央に配置します。

これが私の基本的なコードです:

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

using (Graphics g = Graphics.FromImage(xImage))
{
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.TextRenderingHint = TextRenderingHint.AntiAlias;
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;

    g.FillEllipse(fillBrush, imageRect.X, imageRect.Y, imageRect.Width - 1, imageRect.Height - 1);

    g.DrawString(Text, font, Brushes.White, imageRect, stringFormat);
}

テキストは水平方向に中央揃えされています...しかし、垂直方向に適切に中央揃えされていません。大文字の「I」のような左右対称の文字を使用すると、文字の上部が常に文字の下部よりも四角形の端に近いことがわかります。距離はおそらく少なくとも 50% 増加します。

小文字の「j」のような文字が下にぶら下がっているのに十分なスペースを測定していると思います。ただ、一文字でグラフィカルなアイコンを作ろうとしているので、もっと正確なセンタリングが欲しいです。

4

3 に答える 3

9

GraphicsPathサイズ計算を実行するために使用します。

public static void DrawCenteredText(Graphics canvas, Font font, float size, Rectangle bounds, string text)
{
    var path = new GraphicsPath();
    path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);

    // Determine physical size of the character when rendered
    var area = Rectangle.Round(path.GetBounds());

    // Slide it to be centered in the specified bounds
    var offset = new Point(bounds.Left + (bounds.Width / 2 - area.Width / 2) - area.Left, bounds.Top + (bounds.Height / 2 - area.Height / 2) - area.Top);
    var translate = new Matrix();
    translate.Translate(offset.X, offset.Y);
    path.Transform(translate);

    // Now render it however desired
    canvas.SmoothingMode = SmoothingMode.AntiAlias;
    canvas.FillPath(SystemBrushes.ControlText, path);
}
于 2011-12-08T19:26:22.543 に答える
1

使用する場合

StringFormat stringFormat = new StringFormat(StringFormat.GenericTypographic);

あなたが持っている

ここに画像の説明を入力

代わりは

ここに画像の説明を入力

お役に立てれば

于 2011-12-08T17:59:49.920 に答える
0

ジョン・アーレンの答えは完璧ですが、私の答えを投稿したいと思います:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
         // Set up string.
        string measureString = "HelloWorld";
        Font stringFont = new Font("Arial", 100, FontStyle.Regular, GraphicsUnit.Pixel);

        // Measure string.
        SizeF stringSize = new SizeF();
        stringSize = e.Graphics.MeasureString(measureString, stringFont);

        // Draw rectangle representing size of string.
        e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 10.0F, 10.0F, stringSize.Width, stringSize.Height);

        // Draw string to screen.
        e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(10, 10f + stringSize.Height / 12.0f));
    }

コードの結果は次のようになります。 私のコンピュータに結果

「HelloWorld」は、赤いボックスの垂直方向の中央に配置されています。

ディセンダー ラインの高さは、次の式でstringSize.Height計算された値の約 1/6 に等しいため、MeasureString


| | トップパディング 1/6

| | 単語本文 3/6

| | 単語ディセンダー 1/6

| | 下のパディング 1/6

于 2014-03-06T12:12:40.040 に答える