5

現在、ビットマップを動的に作成し、ビットマップのグラフィックス オブジェクトを使用して、次のように文字列を描画しています。

System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.DrawString(text, font, brush, new System.Drawing.Point(0, 0));

これは、文字列が左から右にまっすぐ書かれた長方形のビットマップを返します。ひもも虹の形に描けるようになりたいです。これどうやってするの?

4

4 に答える 4

18

私は最近この問題を抱えていました (私は CD に印刷するテキストをレンダリングしていました)、ここに私の解決策があります:

private void DrawCurvedText(Graphics graphics, string text, Point centre, float distanceFromCentreToBaseOfText, float radiansToTextCentre, Font font, Brush brush)
{
    // Circumference for use later
    var circleCircumference = (float)(Math.PI * 2 * distanceFromCentreToBaseOfText);

    // Get the width of each character
    var characterWidths = GetCharacterWidths(graphics, text, font).ToArray();

    // The overall height of the string
    var characterHeight = graphics.MeasureString(text, font).Height;

    var textLength = characterWidths.Sum();

    // The string length above is the arc length we'll use for rendering the string. Work out the starting angle required to 
    // centre the text across the radiansToTextCentre.
    float fractionOfCircumference = textLength / circleCircumference;

    float currentCharacterRadians = radiansToTextCentre + (float)(Math.PI * fractionOfCircumference);

    for (int characterIndex = 0; characterIndex < text.Length; characterIndex++)
    {
        char @char = text[characterIndex];

        // Polar to cartesian
        float x = (float)(distanceFromCentreToBaseOfText * Math.Sin(currentCharacterRadians));
        float y = -(float)(distanceFromCentreToBaseOfText * Math.Cos(currentCharacterRadians));

        using (GraphicsPath characterPath = new GraphicsPath())
        {
            characterPath.AddString(@char.ToString(), font.FontFamily, (int)font.Style, font.Size, Point.Empty,
                                    StringFormat.GenericTypographic);

            var pathBounds = characterPath.GetBounds();

            // Transformation matrix to move the character to the correct location. 
            // Note that all actions on the Matrix class are prepended, so we apply them in reverse.
            var transform = new Matrix();

            // Translate to the final position
            transform.Translate(centre.X + x, centre.Y + y);

            // Rotate the character
            var rotationAngleDegrees = currentCharacterRadians * 180F / (float)Math.PI - 180F;
            transform.Rotate(rotationAngleDegrees);

            // Translate the character so the centre of its base is over the origin
            transform.Translate(-pathBounds.Width / 2F, -characterHeight);

            characterPath.Transform(transform);

            // Draw the character
            graphics.FillPath(brush, characterPath);
        }

        if (characterIndex != text.Length - 1)
        {
            // Move "currentCharacterRadians" on to the next character
            var distanceToNextChar = (characterWidths[characterIndex] + characterWidths[characterIndex + 1]) / 2F;
            float charFractionOfCircumference = distanceToNextChar / circleCircumference;
            currentCharacterRadians -= charFractionOfCircumference * (float)(2F * Math.PI);
        }
    }
}

private IEnumerable<float> GetCharacterWidths(Graphics graphics, string text, Font font)
{
    // The length of a space. Necessary because a space measured using StringFormat.GenericTypographic has no width.
    // We can't use StringFormat.GenericDefault for the characters themselves, as it adds unwanted spacing.
    var spaceLength = graphics.MeasureString(" ", font, Point.Empty, StringFormat.GenericDefault).Width;

    return text.Select(c => c == ' ' ? spaceLength : graphics.MeasureString(c.ToString(), font, Point.Empty, StringFormat.GenericTypographic).Width);
}

プログラム出力の曲線テキストのスクリーンショット

于 2012-06-22T06:53:25.780 に答える
1

唯一の方法は、各キャラクターを個別にレンダリングして、

Graphics.RotateTransform

テキストを回転します。回転角度とレンダリング オフセットを自分で計算する必要があります。を使用できます。

Graphics.MeasureCharacterRanges

各文字のサイズを取得します。

于 2010-05-10T15:26:40.080 に答える
0

残念なことに、GDI+ では文字列をパスにアタッチする方法がありません (これはあなたが探しているものです)。

したがって、これを行う唯一の方法は「手動」で行うことです。これは、文字列を文字に分割し、独自のパス計算に基づいて配置することを意味します。

これに多くの作業を加えたくない場合は、これを行うためのライブラリ (潜在的に完全な GDI+ の置き換え) を見つけるか、虹をあきらめる必要があります。

WPF を使用すると、パス上にテキストをレンダリングできます (ハウツーについてはリンクを参照してください) 。

于 2010-05-10T15:26:44.033 に答える