Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算DXF的文本宽度_C#_Text Rendering_Dxf - Fatal编程技术网

C# 计算DXF的文本宽度

C# 计算DXF的文本宽度,c#,text-rendering,dxf,C#,Text Rendering,Dxf,我正在使用netDXF()生成用于AutoCAD的DXF文件。然而,我有一个问题是如何正确设置多行文字的宽度。我希望能够定义文本应该适合的宽度,并更改文本的宽度因子(水平挤压),使其适合定义的区域。因此,如果我有一个40mm的宽度来适应文本,并且文本是80mm长,那么它需要有一个0.5的宽度因子。唯一的问题是我不知道如何准确地确定文本的宽度。我尝试了以下方法,但未获得正确的结果: 我已附上我的代码。我基本上是用3种方法中的每种方法打印一条水平线来计算文本宽度,并将其与实际文本宽度进行比较。

我正在使用netDXF()生成用于AutoCAD的DXF文件。然而,我有一个问题是如何正确设置多行文字的宽度。我希望能够定义文本应该适合的宽度,并更改文本的宽度因子(水平挤压),使其适合定义的区域。因此,如果我有一个40mm的宽度来适应文本,并且文本是80mm长,那么它需要有一个0.5的宽度因子。唯一的问题是我不知道如何准确地确定文本的宽度。我尝试了以下方法,但未获得正确的结果:

我已附上我的代码。我基本上是用3种方法中的每种方法打印一条水平线来计算文本宽度,并将其与实际文本宽度进行比较。如果我改变字体,我会得到不同的结果。我附上了两张图片。一个使用Calibri代码,另一个使用Arial代码。我需要的行是在文本的边缘,无论我使用什么字体

这是我的密码:

public void TestMethod1()
        {
            Application.SetCompatibleTextRenderingDefault(false);

            //text width in mm
            float textWidth = 40;
            float textHeight = 200;
            string labelText = "HELLO WORLD!";
            TextStyle textStyle = new TextStyle("Calibri");

            DxfDocument dxf = new DxfDocument();
            Layer layer1 = new Layer("layer1");
            layer1.Color = new AciColor(0, 0, 255);
            layer1.Name = "Text";

            MText text1 = new MText(new Vector2(0, 0), textHeight, 0, textStyle);
            text1.Layer = layer1;
            text1.AttachmentPoint = MTextAttachmentPoint.MiddleCenter;

            //Will the text fit in the bounds of the rectangle? If not change width factor so it does.
            Font f = new Font(textStyle.FontName, textHeight);
            Size size = TextRenderer.MeasureText(labelText, f);
            SizeF sizeF = graphicsMeasureString(labelText, f);
            int width = MeasureDisplayStringWidth(labelText, f);

            float widthFactor = Math.Min(1, textWidth / sizeF.Width);
            MTextFormattingOptions mtextOptions = new MTextFormattingOptions(text1.Style);
            //mtextOptions.WidthFactor = widthFactor;
            text1.Write(labelText, mtextOptions);

            //Red, g.MeasureString
            Line line1 = new Line(new Vector2(0 - sizeF.Width / 2, 0), new Vector2(0 + sizeF.Width / 2, 0));
            line1.Color = new AciColor(255, 0, 0);

            //Green, TextRenderer
            Line line2 = new Line(new Vector2(0 - size.Width / 2, 5), new Vector2(0 + size.Width / 2, 5));
            line2.Color = new AciColor(0, 255, 0);

            //Yellow, MeasureDisplayStringWidth
            Line line3 = new Line(new Vector2(0 - width / 2, -5), new Vector2(0 + width / 2, -5));
            line3.Color = new AciColor(255, 255, 0);

            dxf.AddEntity(text1);
            dxf.AddEntity(line1);
            dxf.AddEntity(line2);
            dxf.AddEntity(line3);
            dxf.Save("Text Width Test.dxf");

        }

        public SizeF graphicsMeasureString(string text, Font f)
        {
            Bitmap fakeImage = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(fakeImage);
            SizeF sizeF = g.MeasureString(text, f, new PointF(100, 0), StringFormat.GenericTypographic);

            return sizeF;
        }

        public int MeasureDisplayStringWidth(string text, Font f)
        {
            Size size = TextRenderer.MeasureText(text, f);
            Bitmap fakeImage = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(fakeImage);

            System.Drawing.StringFormat format = new System.Drawing.StringFormat();
            System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, 1000, 1000);
            System.Drawing.CharacterRange[] ranges = { new System.Drawing.CharacterRange(0, text.Length) };
            System.Drawing.Region[] regions = new System.Drawing.Region[1];

            format.SetMeasurableCharacterRanges(ranges);

            regions = g.MeasureCharacterRanges(text, f, rect, format);
            rect = regions[0].GetBounds(g);

            return (int)(rect.Right + 1.0f);
        }