System.Drawing.DrawString()长字符串的奇怪包装 [英] System.Drawing.DrawString() weird wrapping of long string

查看:146
本文介绍了System.Drawing.DrawString()长字符串的奇怪包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台应用程序内部的图像上绘制的文本的格式存在一些小问题。我要绘制的文本是:

I'm having some slight issues with the formatting of the text drawn on an image inside of my console application. The text I'm trying to draw is:


BAS2016 = PTR = E30BAS2010 =(S20)$ W30 $ PTO2016 = N5W20N5(W20N10)(S10W20)S5W5S5E10N10 (E15N5)(S5E15)S10E25 $ W25N10(W15N5)(S5W15)S10W10S15BAS2020 = S15PTO2013 = S5E20S5(E20S10)(N10E20)N5E5N5W10S10(W15S5)(N5W15)N10W15 $ 15E5S15E15N10W25 $ E15S5) >

BAS2016=PTR=E30BAS2010=(S20)$W30$PTO2016=N5W20N5(W20N10)(S10W20)S5W5S5E10N10(E15N5)(S5E15)S10E25$W25N10(W15N5)(S5W15)S10W10S15BAS2020=S15PTO2013=S5E20S5(E20S10)(N10E20)N5E5N5W10S10(W15S5)(N5W15)N10W25$E25S10(E15S5)(N5E15)N10E10N15W65$E65N15$.

我的方法调用是:

RectangleF rectF = new RectangleF(0, 0, 320, 320);

graphics.DrawString(fullTrav, defaultFont, Brushes.Black, rectF);

输出结果是:

此输出仅具有默认的 StringFormat 长方形。它可以很好地包装,但是似乎要对字符进行不同的处理,并根据边框中即将出现的字符将换行符插入边框前。我尝试了 StringFormatFlag.FitBlackBox 无济于事,但没有遍历每个标志。

This output is only with whatever is the default StringFormat for the Rectangle. It wraps fine but it seems to treat characters differently and pushes newlines before the border, depending on what characters are upcoming. I've tried StringFormatFlag.FitBlackBox to no avail but haven't gone through each and every flag.

我想要得到的是:

此所需的输出是

推荐答案

由@BradleyUffner协助的答案。


解决此问题的唯一方法是测量字符串的每个字符,当行长达到极限时切断该行,然后单独打印每行。

Answer facilitated by @BradleyUffner.

The only solution to this problem was to measure each character of the string, cutting off the line when the line length hit the limit, and then printing each line individually.

代码如下:

using (var graphics = Graphics.FromImage(bmp))
{
    PointF ptF = new PointF(last.textPoints.subCode.X, last.textPoints.subCode.Y + 40);

    int lineIndex = 0;
    double lineHeight = defaultFont.Height;

    while (fullTrav.Length > 0)
    {
        float lineLength = 0;
        int currentChar = 1;
        while (lineLength < 320 && currentChar <= fullTrav.Length)
        {
            string line = fullTrav.Substring(0, currentChar);
            lineLength = graphics.MeasureString(line, defaultFont).Width;
            currentChar++;
        }

        //
        // Optional Code Start
        //
        while (fullTrav[currentChar - 2].ToString().IndexOfAny("(NSEW".ToCharArray()) != -1)
        {
            currentChar--;
        }

        if (currentChar - 1 < fullTrav.Length)
        {
            while (fullTrav[currentChar - 1].ToString().IndexOfAny("1234567890".ToCharArray()) != -1)
            {
                currentChar--;
            }
        }
        //
        // Optional Code End
        //

        graphics.DrawString(fullTrav.Substring(0, currentChar - 1), defaultFont, Brushes.Black, ptF.X, ptF.Y + lineIndex * 20, StringFormat.GenericDefault);

        fullTrav = fullTrav.Substring(currentChar - 1);
        lineIndex++;
        currentChar = 1;

    }
}

此代码包括我使用过的可选区域o仅中断特定字符。稍微解释一下,它防止它分裂成 N4。或 W17在两个不同的行之间,它可以防止起始括号(成为行中的最后一个字符。

This code includes and optional area that I used to break on specific characters only. To explain a little, it prevents it from splitting up "N4" or "W17" between two different lines and it prevents a beginning parenthesis "(" from being the last character in a line.

成品看起来像:

这篇关于System.Drawing.DrawString()长字符串的奇怪包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆