简体   繁体   English

C#字体字符渲染(GDI错误)?

[英]C# Font character rendering (GDI Bug)?

I am trying to draw the character "t" to a bitmap at location (0,0), then save the bitmap. 我试图将字符“ t”绘制到位置(0,0)的位图中,然后保存该位图。 However, whenever I do this, the character is rendered at x = 8 (for size 42). 但是,无论何时执行此操作,字符都将以x = 8呈现(对于大小42)。 The offset can be different per font size, it is 6 when the size is 36. This seems to be a bug related to GDI. 偏移量可能因字体大小而异,当字体大小为36时,偏移量为6。这似乎是与GDI相关的错误。 How can I query this value that is creating this offset? 如何查询正在创建此偏移量的值? I tried "GetCharABCWidthsFloat", but the left and right bearings will be zero. 我尝试了“ GetCharABCWidthsFloat”,但是左右方向的轴承将为零。 I tried "GetTextMetrics", but that does not help. 我尝试了“ GetTextMetrics”,但这无济于事。 I noticed this article mentions it, but does not mention how to fix it http://support.microsoft.com/kb/307208 我注意到本文提到了它,但没有提到如何解决它http://support.microsoft.com/kb/307208

As a testing measure, I tried to render t at (-8, 0) and it ended up rendering at (0, 0). 作为测试措施,我尝试在(-8,0)处渲染t,最终在(0,0)处渲染。 Does anyone know how I can get this offset value? 有谁知道我如何获得这个偏移值? I am not interested in hacking something together, but a real viable solution. 我对将某些东西一起黑客攻击不感兴趣,但是真正可行的解决方案。

GetMeasureString, GetLineSpacing does not help. GetMeasureString,GetLineSpacing无济于事。

Example Code: 示例代码:

static class Program
{
    static void Main(string[] args)
    {
        FontStyle style = FontStyle.Regular;
        FontFamily fontFamily = new FontFamily("Times New Roman");
        Font font = new Font("Times New Roman", 48, style);

        Bitmap bitmap = new Bitmap(64, 64);
        Graphics graphic = Graphics.FromImage(bitmap);

        SolidBrush blackBrush = new SolidBrush(Color.Black);
        graphic.FillRectangle(blackBrush, 0, 0, 64, 64);
        graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

        SizeF size = graphic.MeasureString("t", font);
        float lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

        graphic.DrawString("t", font, Brushes.White, new Point(0, 0));
        bitmap.Save("t.png");
        graphic.Dispose();
    }
}

试试StringFormat.GenericTypographic

graphic.DrawString("t", font, Brushes.White, new Point(0, 0), StringFormat.GenericTypographic);

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

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