C# Graphics.DrawString 不渲染特殊字符?
我正在尝试向自定义控件写入一些任意文本。
我的代码可以工作,但是当我尝试绘制 ü 等字符时,它会在该字符所在的位置显示一个空方块。
我需要这个按照我打算支持本地化的方式工作。
我已经检查过,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的字体不包含该字形,您将看到一个矩形。你知道事实并非如此,Tahoma 绝对有 ü。这意味着真正的问题是您没有正确地识别该文件。
您需要了解文件中的文本是如何编码的。您知道它不是 UTF8,这是 StreamReader 的默认设置。您的下一个猜测可能应该是:
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:
确保字体支持字符(使用字符映射或其他东西)并使用unicode。
例如: \u00FC 应该是您要查找的字符
以 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: