如何在形状中绘制文字? C#

时间:2017-04-16 15:31:10

标签: c# winforms graphics

问题很简单,假设你有一个形状(比方说是一个矩形)和一个文本("你好"例如),所以它会把所有的文字都写在矩形适合的次数,例如:

hello hello hello hello

hello             hello

hello             hello

hello hello hello hello

为了做到这一点,我假设您需要使用图形变量,我只是不知道该怎么做。

用于在位图对象中绘制字符串的代码:

Bitmap tempp = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(tempp);
SizeF w = g.MeasureString("22", new Font("Tahoma", 200));//in order to get the size of the string as a pixel measurement
Bitmap bmp = new Bitmap((int)w.Width+1, (int)w.Height+1);//the bitmap that will contain the text as a picture
RectangleF rectf = new RectangleF(0, 0, (int)w.Width+1, (int)w.Height+1);
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
    Alignment = StringAlignment.Center,
    LineAlignment = StringAlignment.Center
};
g.DrawString("22", new Font("Tahoma", 200), Brushes.Black, rectf, format);
g.Flush();

提前致谢。

第二条评论:

hello hello hello
hel           llo
hel           llo
hello hello hello

1 个答案:

答案 0 :(得分:1)

我希望这就是你所需要的。这里没有太多要解释的。逻辑很简单。

    public string MyString = "Hello"
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        var g = e.Graphics;
        var strFont = new Font("Tahoma", 10);
        var strSizeF = g.MeasureString(MyString, strFont);

        var canvas = new Rectangle
        {
            Height = 200,
            Width = 200,
            Location = new Point(10, 10),
        };

        g.DrawRectangle(new Pen(new SolidBrush(Color.Blue)), canvas);

        var nx = (int)(canvas.Width / strSizeF.Width);
        var ny = (int)(canvas.Height / strSizeF.Height);
        var spacingX = (canvas.Width - nx * strSizeF.Width) / (nx-1);
        var spacingY = (canvas.Height - ny * strSizeF.Height) / (ny-1);

        //draw top row and bottom row
        int i;
        for (i = 0; i < nx; i++)
        {
            g.DrawString(
                MyString,
                strFont,
                Brushes.Black,
                new PointF(canvas.X + i*(strSizeF.Width + spacingX), canvas.Y)
                );
            g.DrawString(
                MyString,
                strFont,
                Brushes.Black,
                new PointF(canvas.X + i * (strSizeF.Width + spacingX), canvas.Y + canvas.Height - strSizeF.Height)
            );
        }

        //divide the string into half
        var isLengthOdd = MyString.Length % 2 != 0;
        var substr1 = MyString.Substring(0, MyString.Length / 2 + (isLengthOdd ? 1 : 0));
        var substr2 = MyString.Substring(MyString.Length / 2, MyString.Length - MyString.Length / 2);
        var substr2SizeF = g.MeasureString(substr2, strFont);
        //draw side rows
        for (i = 1; i < ny - 1; i++)
        {
            g.DrawString(
                substr1,
                strFont,
                Brushes.Black,
                new PointF(canvas.X, canvas.Y + i * (strSizeF.Height + spacingY))
            );
            g.DrawString(
                substr2,
                strFont,
                Brushes.Black,
                new PointF(canvas.X + canvas.Width - substr2SizeF.Width, canvas.Y + i * (strSizeF.Height + spacingY))
            );
        }

    }

结果:

Result