How to solve representation/encoding errors in C# Winforms applications

I know the title of this post hurts, but I wasn’t able to describe the problem with fewer words. But let us get to the point. As part of the development of my cloud downloader I encountered (among much others) the following problem. Korean characters were displayed as blank boxes, even though they were shown in the source code editor correctly. Those of you, who wonder now,  from where you can get the Korean characters on your keyboard, should just stop by Google Translate. (The screenshot below illustrates the problem once again. Please click to enlarge the image.)

Koreanische Zeichen falsche Darstellung

On the Internet I found only very few solutions. And almost all were, in my opinion, rather suboptimal. In most cases the following solution was proposed:

It is recommended that you call the SetCompatibleTextRenderingDefault() method with the parameter true. In this way, texts are rendered using a GDI+ based method, which was used in the days of .Net Framework 1.x.
However, this way brings some “disadvantages” with it. First, the measures by using the above method are calculated differently, so your existing layout may “fall apart” and perhaps some of your controls won’t be rendered correctly. (From .Net 2.0 GDI-based textrenderer class was introduced.)

If we would use the above way on our test application the result would look like in the following.

In the Program.cs we activate the DefaultTextRendering.

 static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);
            Application.Run(new Form1());
        }
    }
}

And in the application we get the following result:
Koreanische Zeichen setcompatibletextrendering(true)
As you can see, you see that only the label is shown correctly. The groupbox renders the characters still wrong.

The other way

Another, and in my opinion, better solution is the way of changing the used font/font-property. There are a few fonts that contain a lot more characters and symbols than all the standard fonts. One of these for example is Arial Unicode MS, which comes with Microsoft Office, but is also avaible on Mac OS X and as separately download.
If you set up this font for all of your application’s controls now, you get the following result:

namespace KoreanCharacters
{
    public partial class Form1 : Form
    {
        // [...]

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Font = new Font("Arial Unicode MS", 8.25f);
            foreach (Control con in GetAllControls(this))
                if (con.Font != null)
                    con.Font = new Font("Arial Unicode MS", 8.25f);

            this.Text = "안녕하세요";
            label1.Text = "안녕하세요";
            groupBox1.Text = "안녕하세요";
        }

        private static List<Control> GetAllControls(Control controlBase)
        {
            List<Control> controls = new List<Control>();
            foreach (Control con in controlBase.Controls)
            {
                controls.Add(con);
                List<Control> subControls = GetAllControls(con);
                controls.AddRange(subControls);
            }
            return controls;
        }
    }
}

As you can see, now the text of the groupbox is also displayed correctly. So all controls, with the exception of the Form.Text-property are displayed correct. That’s why I described this possibility simply as “better” and not as “perfect” solution. Why the title of the Forms is not rendered proper, I can not explain myself, too.

I hope I could help the one or the other of you. Did you ever faced the same problem before? And how did you solve it? Do you know maybe even a better solution? I appreciate every comment!

Greetings,
Raffi 

1 Comments

  1. It is one of the frequent errors on any Windows-based platforms regarding any encoding issues. For better suggestions can check from that would be more fruitful for them to resolve their issues easily.

Leave a Reply to Sign In 0x87dd0006 Cancel reply

Please be polite. We appreciate that. Your email address will not be published and required fields are marked