views:

125

answers:

2

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?

A: 

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);
Blam
Thanks, that works when hardcoded!However, I'm loading my strings from an external file, and can't escape like that.
Origamiguy
Edited answer, not too sure if reading in as unicode will work, but it should.
Blam
Nope, didn't work. Don't worry, fixed it now anyhow. Encoding.Default works (:
Origamiguy
+1  A: 

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...
Hans Passant
Here, have a cookie! It worked. Interestingly, the integer value of the char was 0xFF01 higher than it should have been when I didn't specify an encoding.
Origamiguy
You hand out cookies at SO by clicking the "this answer is useful" button. It's the hat above the zero.
Hans Passant