同一台计算机上不同进程下的GDI +字体大小差异 [英] GDI+ font size difference under different processes on same machine

查看:59
本文介绍了同一台计算机上不同进程下的GDI +字体大小差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表述问题:

当我从Visual Studio中的单元测试中绘制位图时,会考虑到我在桌面上具有的缩放系数,但是当我从控制台运行时却没有考虑.

When I draw on bitmaps from a unit test from within Visual Studio, the zoom factor I have on my desktop is taken into account, but when I run from the console it is not.

无论我的桌面设置如何,如何使使用Graphics.DrawString呈现的文本表现一致?

What can I do to make texts rendered using Graphics.DrawString behave consistently regardless of my desktop settings?

我有一些NUnit测试,这些测试使用GDI +创建位图,并将它们与预先保存的图像进行比较,以确保生成正确的图像.

I have some NUnit tests that create bitmaps using GDI+ and compares them to pre-saved images to make sure that the correct image is generated.

我的问题是,当我从Visual Studio中运行测试时,文本(用Graphics.DrawString呈现)与运行然后在命令行构建脚本中进行测试(使用NUnit 3.2测试运行程序)时生成的文本不同).

My problem is that texts (rendered with Graphics.DrawString) are different when I run the tests from within Visual Studio, compared to what is generated when I run then tests in my command line build script (using the NUnit 3.2 test runner).

注意:这不是一些小的1像素偏移或文本换行问题.从控制台运行时呈现的文本的字体大小较小.

Note: it is not some minor one-pixel-off or text wrapping issue. The font size of the texts rendered while running from the console is waaay smaller.

有人知道为什么字体大小在同一台计算机上会根据执行环境而有所不同吗?同样重要的是:我该怎么办?

Does anybody know why the font size would be different one the same machine depending on the execution environment? And equally important: what can I do about it?

推荐答案

几乎从未声明过控制台模式应用程序 dpiAware .当您创建位图时,这很重要,无论视频适配器运行在什么位置,它们的默认水平/垂直分辨率"属性将为每英寸96像素.DPI虚拟化功能可以解决这一问题.这反过来会影响字体使用的像素数,因为默认情况下,其大小以磅为单位指定.1点是1/72英寸,您需要绘制多少像素(例如9点字体)取决于像素/英寸的比率.这将是9/72 * 96 = 12像素.

A console mode app is almost never declared dpiAware. That matters when you create bitmaps, their default Horizontal/VerticalResolution property will be 96 pixels per inch regardless of what the video adapter is running at. The DPI virtualization feature takes care of that. Which in turn affects the number of pixels used for a font since by default their size is specified in points. One point is 1/72 inches, how many pixels you need to draw, say, an 9 point font depends on the pixels-per-inch ratio. It will be 9 / 72 * 96 = 12 pixels.

在运行144 dpi视频适配器的dpiAware程序中,相同文本的高度为9/72 * 144 = 18像素.导致您得出结论,控制台模式应用程序使文本太小.还是dpiAware应用程序使它们过大,具体取决于您的观点.

In a program that is dpiAware that runs on, say, a 144 dpi video adapter, that same text will be 9 / 72 * 144 = 18 pixels high. Leading you to conclude that the console mode app is making the text too small. Or the dpiAware app is making them too big, depending on your point of view.

DPI意识或虚拟化通常很重要,它可以防止程序在高分辨率监视器上创建不可读的文本.在4K显示器上,高12像素的文本看起来像灰尘斑点.这不适用于创建位图的控制台模式应用程序.尽管您可能会考虑创建更大的位图,因此也使用更大的文本.因为如果您不这样做,那些小的位图在必须重新缩放以匹配4K显示器时看起来就不会很好.抗锯齿像素对于文字特别显着,变成了不再抗锯齿的巨石.

DPI awareness or virtualization is general important, it prevents a program from creating unreadable text on a high resolution monitor. On a 4K monitor that 12 pixel high text looks but like a fleck of dust. That does not exactly apply in a console mode app that creates bitmaps. Although you might well consider to create much larger bitmaps and therefore also use larger text. Since if you don't those small bitmaps don't look very good when they have to be rescaled to match the 4K monitor. Especially noticeable for text, anti-aliasing pixels turn into boulders that don't anti-alias anymore.

如果不是,并且您只关心像素大小,则应考虑通过使用采用 GraphicsUnit 的Font构造函数来避免此问题.您可能希望使用GraphicsUnit.Pixel而不是默认值.无论视频适配器设置如何,您都会得到可预测的文本高度(以像素为单位),占总位图高度的固定比例.

If not, and you only care about sizes in pixels, you should consider avoiding this problem is by using one of the Font constructors that takes a GraphicsUnit. You would favor GraphicsUnit.Pixel instead of the default. You'll get a predictable text height in pixels regardless of the video adapter setting, a fixed proportion of the overall bitmap height.

这篇关于同一台计算机上不同进程下的GDI +字体大小差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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