简体   繁体   English

在位图中创建Windows设置(DPI)独立文本

[英]Create Windows settings (DPI) independent text in Bitmaps

I am wondering if it is possible to create Bitmaps containing text in C# (.NET 4.0) where the text is totally independent of any Windows configuration or settings. 我想知道是否可以在C#(.NET 4.0)中创建包含文本的位图,其中文本完全独立于任何Windows配置或设置。

I am trying around with that issue for weeks but did not really find a solution. 我几周来一直试图解决这个问题,但并没有真正找到解决方案。 To investigate the facts I have created a small console application in VS2010 containing the following code: 为了调查事实,我在VS2010中创建了一个包含以下代码的小型控制台应用程序:

class Program
{
    private const TextFormatFlags DefaultTextFormatFlags = TextFormatFlags.NoPadding | TextFormatFlags.Left | TextFormatFlags.NoPrefix;

    private static int[] aPpi = { 72, 96, 192 };
    private static bool[] aSetPpi = { false, true };
    private static GraphicsUnit[] aGU = { GraphicsUnit.Pixel, GraphicsUnit.Point };
    private static int[] aSize = { 10, 14, 100, 200 };

    static void Main(string[] args)
    {
        int idx = 0;

        foreach (int ppi in aPpi)
        {
            foreach (bool b in aSetPpi)
            {
                foreach (GraphicsUnit gu in aGU)
                {
                    foreach (int size in aSize)
                    {
                        idx++;
                        CreatePic(idx, ppi, b, gu, size);
                    }
                }
            }
        }
    }

    private static void CreatePic(int id, int ppi, bool setppi, GraphicsUnit unit, int size)
    {
        Bitmap bm;

        Graphics g;
        Font font;
        Color txtColor;
        Color background;
        string fn;

        txtColor = Color.Blue;
        background = Color.Transparent;

        bm = new Bitmap(1000, 1000,  PixelFormat.Format32bppArgb);

        if (setppi)
        {
            bm.SetResolution(ppi, ppi);
        }

        g = Graphics.FromImage(bm);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        g.PageUnit = GraphicsUnit.Pixel;

        font = new Font("Roboto Lt", size, FontStyle.Regular, unit);

        TextRenderer.DrawText(g, "0T1ÄgÁj", font, new Point(0, 0), txtColor, background, DefaultTextFormatFlags);

        font.Dispose();

        g.Dispose();

        fn = string.Format("p_{0,-5}_{1,-4}_{2,-4}_{3,-4}.png", unit, size, setppi, ppi);
        bm.Save(fn, ImageFormat.Png);
        bm.Dispose();
    }
}

It creates PNG files each containing a test text in several combinations of font size, bitmap resolution and font size unit. 它创建PNG文件,每个文件包含几种字体大小,位图分辨率和字体大小单位组合的测试文本。 At first I executed it while Windows was set to normal font size (Screen resolution setting to 100%), the second run was done with Screen resolution set to 125%. 首先我执行它,而Windows设置为正常字体大小(屏幕分辨率设置为100%),第二次运行完成屏幕分辨率设置为125%。

I basically had 2 expectations: 我基本上有2个期望:

  1. When the Font size is specified with "Pixel" as unit, the windows resolution does not have any influence on the result in the bitmap. 当以“像素”为单位指定字体大小时,窗口分辨率对位图中的结果没有任何影响。 It will be always the same for same font sizes also independent of the resolution of the bitmap. 对于相同的字体大小,它也将始终相同,也与位图的分辨率无关。
  2. When the Font size is specified with "Point" as unit and additionally the Bitmap resolution was explicitly set, the Windows screen resolution setting does not have any influence on the result in the bitmap. 如果以“点”为单位指定字体大小,并且另外明确设置了位图分辨率,则Windows屏幕分辨率设置对位图中的结果没有任何影响。

But the results did not meet my expectations. 但结果并没有达到我的预期。 1. When the Font size was specified with "Pixel" as unit, the windows resolution caused different results for 14px size, but not for 10px, 100px or 200px. 1.当以“像素”为单位指定字体大小时,窗口分辨率导致14px大小的不同结果,但不是10px,100px或200px。 2. When the Font size is specified with "Point" as unit it did not have any effect to set the Bitmap resolution explicitly. 2.当使用“Point”作为单位指定字体大小时,显式设置位图分辨率没有任何效果。

Until now I did not found any explanation for that. 到目前为止,我没有找到任何解释。

Can anyone tell me how I can create Text in Bitmaps always looking like Windows resolution is set to 100% even if it is not? 任何人都可以告诉我如何在位图中创建文本总是看起来像Windows分辨率设置为100%,即使它不是?

Thank you in advance! 先感谢您!

Take a look at the Win32 API LogFont structure and the lfHeight member to get an idea as to how windows is fulfilling your request for height. 查看Win32 API LogFont结构和lfHeight成员,了解Windows如何满足您的身高要求。 Basically you'll want to use the equation given there and a constant DPI. 基本上你会想要使用那里给出的等式和恒定的DPI。 You're specifying a screen device context so when you change from 100% to 125% etc.. the DPI of the screen is changing and affecting the size of the font returned by the Windows font mapper. 您正在指定屏幕设备上下文,因此当您从100%更改为125%等时,屏幕的DPI正在发生变化,并影响Windows字体映射器返回的字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM