gpt4 book ai didi

c# - 使用相同 X&Y 的 DrawString 和 DrawRectangle 从不同的地方开始

转载 作者:行者123 更新时间:2023-11-30 13:05:00 24 4
gpt4 key购买 nike

我有以下代码:

public void DrawLetter(int Cell, string Letter, int X, int Y)
{
System.Drawing.Font fBody = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

Pencil.DrawString(Letter, fBody, System.Drawing.Brushes.Black, X, Y);
Pencil.DrawRectangle(new Pen(System.Drawing.Brushes.Red), X, Y, 10, 10);
}

当我经历两次并使用 140,249 和 296,249 作为坐标时,矩形出现在正确的位置,但字符串出现了几个像素,我不知道为什么。

我放入 DrawRectangle 以检查它是否从正确的位置开始,确实如此。我在 DrawString 中做错了什么吗?另外,如果我画一个 D 和一个 Z,D 的宽度是 10px,Z 是 8px,我认为使用 Courier 会给我固定的宽度吗?

更新:我查看了 MSDN 上的示例这是一个截图。即使 DrawString 位于 0,0,您也可以看到该字母并未出现在 0,0。矩形确实如此。必须有padding什么的:

Screenshot

更新 2: 使用以下代码似乎有所改善,尽管它并不完美:

StringFormat strFormat = new StringFormat(StringFormat.GenericTypographic);
Pencil.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
Pencil.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), strFormat);

Screenshot 2

最佳答案

“Courier New”字体不会考虑默认的填充/行高等吗?

关于c# - 使用相同 X&Y 的 DrawString 和 DrawRectangle 从不同的地方开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6599468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com