Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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# 如何使用TextMetrics测量字符串的维度_C#_.net - Fatal编程技术网

C# 如何使用TextMetrics测量字符串的维度

C# 如何使用TextMetrics测量字符串的维度,c#,.net,C#,.net,我想知道一根绳子的高度。我遇到过测量,但它对于我需要它的东西来说是不准确的-对于MS Word显示的东西来说太小了好吧,这可能是另一个主题,所以如果你能给我指出正确的资源 所以,我发现了TextMetrics。我想试试,但我不知道如何使用它。以下是我的示例代码: static public double MeasureGroup2() { TextMetrics txt = new TextMetrics(); double height;

我想知道一根绳子的高度。我遇到过测量,但它对于我需要它的东西来说是不准确的-对于MS Word显示的东西来说太小了好吧,这可能是另一个主题,所以如果你能给我指出正确的资源

所以,我发现了TextMetrics。我想试试,但我不知道如何使用它。以下是我的示例代码:

static public double MeasureGroup2()
    {
        TextMetrics txt = new TextMetrics();

        double height;

        height = txt.Height;

        return height;
    }
在何处/如何设置字体和字号以及用于返回所需值的字符串

另外,我的最终目标是测量字符串的高度,就像MS Word测量或渲染字符串一样。除了这两个,我对其他解决方案持开放态度

其他信息:

测量代码:

 static public double MeasureGroup1(string TextFont, int FSize)
    {
        string Str = "Mgkfps";

        Bitmap Bmp = new Bitmap(99,99);
        Graphics DrawGraphics = Graphics.FromImage(Bmp);
        GraphicsUnit Unit = GraphicsUnit.Point;
        DrawGraphics.PageUnit = Unit;
        DrawGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        StringFormat format = StringFormat.GenericTypographic;

        // Set up string.
        System.Drawing.Font stringFont = new System.Drawing.Font(TextFont, FSize, FontStyle.Regular, Unit);

        // Measure string.
        SizeF stringSize = new SizeF();
        stringSize = DrawGraphics.MeasureString(Str, stringFont, 99, format);

        DrawGraphics.Dispose();
        format.Dispose();
        stringFont.Dispose();


        return stringSize.Height;
        //return TextSize;
    }
注: 使用Arial 11和Arial 12,此代码的输出需要乘以1.0294,才能与MS Word的显示方式相同。物理测量是使用Word的标尺、Photoshop和大量的比率和比例进行的。也许这就是问题所在

static public double MeasureGroup2(string TextFont, int FSize)
    {
        string Str = "Mgkfps";
        double height;

        Bitmap Bmp = new Bitmap(99, 99);
        Graphics DrawGraphics = Graphics.FromImage(Bmp);

        GraphicsUnit Unit = GraphicsUnit.Point;
        DrawGraphics.PageUnit = Unit;
        DrawGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
        StringFormat format = StringFormat.GenericTypographic;

        // Set up string.
        System.Drawing.Font stringFont = new System.Drawing.Font(TextFont, FSize, FontStyle.Regular, Unit);

        Rectangle Rect = new Rectangle(0, 0, 99, 99);

        Size sz = new Size(99,99);
        sz = TextRenderer.MeasureText(DrawGraphics, Str, stringFont, sz ,TextFormatFlags.NoPadding);

        height = sz.Height;

        return height;
    }

首先,谢谢大家帮助我

使用TextMetrics获取文本的维度时,请参阅以下文章:

他在页面底部有一个示例项目

至于我希望在哪里使用它,即在MS Word中获取文本的高度:

我假设文本高度和行距在设置为single时是相同的。事实并非如此。有一小部分空间,即前导,不是通过测量来测量的。这就是为什么我希望使用GetTextMetrics函数,因为它返回一个包含前导的结构。不管怎样,它似乎被用于其他用途

幸运的是,显然,.NET framework有一个本机方法返回我需要的值,即FontFamily.GetLineSpacing

LineHeight = stringFont.Size * FF.GetLineSpacing(FontStyle.Regular)/FF.GetEmHeight(FontStyle.Regular);
参考:


用于保存的返回值。我不认为那是你想要的。有用吗?测量是不准确的,不。它的使用方式可能不准确。但是,请参见。在这里提出建议是困难的。您尚未定义清晰的上下文。@pm100是的,谢谢。我以前读过并测试过这篇文章,但没有看过这篇文章。MeasureString为我的案例提供了更精确的测量。@Jimi,它的使用方式可能不准确。有趣的我想知道更多关于这方面的情况。我更新了我的帖子,使用MeasureString包含了代码。