开发者

C# Graphics.DrawString not rendering special characters?

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?


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜