C# Graphics.DrawString 不渲染特殊字符?

发布于 2024-09-14 18:45:17 字数 496 浏览 2 评论 0原文

我正在尝试向自定义控件写入一些任意文本。
我的代码可以工作,但是当我尝试绘制 ü 等字符时,它会在该字符所在的位置显示一个空方块。
我需要这个按照我打算支持本地化的方式工作。
我已经检查过,Tahoma 具有所需的字符。我的代码如下:

string _Label = "Zurück";
Font labelFont = new Font("Tahoma", 9, FontStyle.Bold);
SizeF labelSize = e.Graphics.MeasureString(_Label, labelFont);
this.Width = (int)(labelSize.Width + this.Padding.Horizontal);
e.Graphics.DrawString(_Label, labelFont, Brushes.White, new PointF(this.Padding.Left, this.Padding.Top));

有人知道如何解决这个问题吗?

I'm trying to write some arbitrary text to a custom control.
My code works, but when I try to draw characters such as ü, it displays an empty square where the character would be.
I need this to work as I intend to support localizations.
I have checked already, and Tahoma has the required characters. My code is below:

string _Label = "Zurück";
Font labelFont = new Font("Tahoma", 9, FontStyle.Bold);
SizeF labelSize = e.Graphics.MeasureString(_Label, labelFont);
this.Width = (int)(labelSize.Width + this.Padding.Horizontal);
e.Graphics.DrawString(_Label, labelFont, Brushes.White, new PointF(this.Padding.Left, this.Padding.Top));

Anyone know how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-09-21 18:45:17

如果您使用的字体不包含该字形,您将看到一个矩形。你知道事实并非如此,Tahoma 绝对有 ü。这意味着真正的问题是您没有正确地识别该文件。

您需要了解文件中的文本是如何编码的。您知道它不是 UTF8,这是 StreamReader 的默认设置。您的下一个猜测可能应该是:

        var sr = new StreamReader(path, Encoding.Default);
        // Read file...

You'll see a rectangle if the font you use doesn't contain the glyph. You know that's not the case, Tahoma definitely has the ü. Which means that the real problem is that you didn't real the file correctly.

You'll need to find out how the text in the file is encoded. You know it isn't UTF8, that's the default for StreamReader. Your next guess probably ought to be:

        var sr = new StreamReader(path, Encoding.Default);
        // Read file...
酒废 2024-09-21 18:45:17

确保字体支持字符(使用字符映射或其他东西)并使用unicode。

例如: \u00FC 应该是您要查找的字符

以 Unicode 形式读取文件:

StreamReader UnicodeFileRead new StreamReader(input, System.Text.Encoding.Unicode);

Makesure the font supports the character (use charmap or something) and use unicode.

Ex: \u00FC should be the character you're looking for

Reading in files as Unicode:

StreamReader UnicodeFileRead new StreamReader(input, System.Text.Encoding.Unicode);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文