为什么TextRenderer.MeasureText使用C#中的大字体无法正确测量文本? [英] Why does TextRenderer.MeasureText not measure text correctly using larger fonts in C#?

查看:395
本文介绍了为什么TextRenderer.MeasureText使用C#中的大字体无法正确测量文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的是获取字符串的像素大小并将其转换为百分之一英寸(即,pixels / DPI =英寸,英寸* 100 =百分之一英寸)。这是我的代码:

What I am doing is getting the pixel size of a string and converting it to hundredths of an inch (ie. pixels/DPI = inches, inches * 100 = hundredths of an inch). Here is my code:

private static SizeF TextSize(string text, Font txtFnt)
{
    SizeF txtSize = new SizeF();

    // The size returned is 'Size(int width, int height)' where width and height
    // are the dimensions of the string in pixels
    Size s = System.Windows.Forms.TextRenderer.MeasureText(text, txtFnt);

    // Value based on normal DPI settings of 96
    txtSize.Width = (float)Math.Ceiling((float)s.Width / 96f * 100f); 
    txtSize.Height = (float)Math.Ceiling((float)s.Height / 96f * 100f);

    return txtSize;
}

现在,使用Arial字体,对于小于12的字体都可以正常工作,但是之后,由于计算出的尺寸小于实际尺寸,因此开始截断字符。我知道我的DPI设置设置为96。所有字体的定义都相同,但字体大小有所不同:

Now, using Arial font this all works fine for fonts smaller that 12, but after that characters start getting cut off because the calculated size is smaller that the actual size. I know that my DPI settings are set at 96. My fonts are all defined the same with the variation in font size:

Font myFont = new Font("Arial", <font size>, FontStyle.Regular, GraphicsUnit.Point);

我相信我必须使用 GraphicsUnit.Point 由于自定义控件,我正在绘制字符串,但是 GraphicsUnit 有关系吗?

I believe that I have to use GraphicsUnit.Point because of the custom control I am drawing the strings to, but does the GraphicsUnit matter?

MeasureText 函数甚至可以正常工作,或者还有其他情况吗?

Is the MeasureText function even working correctly, or is there something else going on?

编辑

我正在绘制一个自定义打印预览控件。打印预览控件中的单位是英寸/ 100(因此转换)。我相信文本,图像等是使用打印机图形对象绘制的,但我不确定。

I am drawing to a custom print preview control. The units in the print preview control are 'Inches/100' (hence the conversion). I believe the text, images, etc are drawn with the Printer graphics object, but I'm not entirely sure.

推荐答案

A很久以前,我遇到了类似的问题,特别是在文本框中测量文本 INSIDE 。问题的症结在于:绘制文本有两种主要方法。 Win32 API和System.Drawing。如果尝试测量文本框内的文本,将使用Win32 API绘制文本,并且还需要使用Win32 API对其进行测量。如果您测量由System.Drawing绘制的文本,则使用.net进行的测量将匹配。

A loong time ago I ran into a similar problem, specifically measuring text INSIDE a textbox. The crux of the matter is this: There are two primary ways of drawing text. The Win32 API and System.Drawing. If you attempt to measure text inside a textbox, it will be drawn using the Win32 API and you will also need to measure it using the Win32 API. If you measure text drawn by System.Drawing, then your measurements using .net will match up.

基本上,窗体文本框使用Win32 API进行布局,绘制和测量还需要来自Win32。

Basically, a Forms textbox uses the Win32 API to layout and draw and measurements also need to come from Win32.

您没有说具体测量的内容和位置,因此此答案甚至可能不适用。

You didn't say what specifically you were measuring and where, so this answer may not even apply.

这篇关于为什么TextRenderer.MeasureText使用C#中的大字体无法正确测量文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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