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#字体字符渲染(GDI错误)?_C#_Fonts_Gdi+_Rendering_Gdi - Fatal编程技术网

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

C#字体字符渲染(GDI错误)?,c#,fonts,gdi+,rendering,gdi,C#,Fonts,Gdi+,Rendering,Gdi,我正在尝试将字符“t”绘制到位置(0,0)处的位图,然后保存位图。但是,每当我执行此操作时,角色将以x=8(大小为42)渲染。偏移量可以根据字体大小而不同,当字体大小为36时,偏移量为6。这似乎是一个与GDI相关的bug。如何查询创建此偏移的此值?我尝试了“GetCharABCWidthsFloat”,但左右方向角将为零。我尝试了“GetTextMetrics”,但没有帮助。我注意到这篇文章提到了它,但没有提到如何修复它 作为测试度量,我尝试在(-8,0)处渲染t,结果它在(0,0)处渲染。有人

我正在尝试将字符“t”绘制到位置(0,0)处的位图,然后保存位图。但是,每当我执行此操作时,角色将以x=8(大小为42)渲染。偏移量可以根据字体大小而不同,当字体大小为36时,偏移量为6。这似乎是一个与GDI相关的bug。如何查询创建此偏移的此值?我尝试了“GetCharABCWidthsFloat”,但左右方向角将为零。我尝试了“GetTextMetrics”,但没有帮助。我注意到这篇文章提到了它,但没有提到如何修复它

作为测试度量,我尝试在(-8,0)处渲染t,结果它在(0,0)处渲染。有人知道如何得到这个偏移值吗?我不想把一些东西拼凑在一起,而是一个真正可行的解决方案

GetMeasureString、GetLineSpacing没有帮助

示例代码:

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.generictyprographic

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

成功了!你知道为什么默认情况下它不这么做吗?