How to draw unicode string using C# graphics.DrawS

2020-07-17 05:58发布

问题:

I'm trying to send khmer script(unicode) string to printer using PrintDocument provided by the .NET framework.

Unfortunately it seems to me that the Graphics.DrawString() does not render khmer script correctly.

Platform: Windows 7 Ultimate
IDE: VS 2010 Ultimate

Here is the sample code:

void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
  var font = new Font("Khmer UI", 12);
  var text = "សួស្តី"; // "Hello"
  e.Graphics.DrawString(text, font, Brushes.Black, 100, 100);
}

回答1:

mann,
I tested your code on a Form_Paint() handler, and I got exactly what you said.
But when I used this instead:

TextRenderer.DrawText(e.Graphics, text, font, new Point(100, 100), Color.Black);  

It gave me the text the way you wanted it.
Try that on your printDoc_PrintPage().



回答2:

Thanks Albin and Beemer for your active response.

After a few posts in c# google group. It's been confirmed that there is a bug in GDI+ that incorrectly show certain script ("Khmer" in this case) to a different wording.

A native win32 test application was created to verify the issue with GDI+ DrawString().

A bug report has been submitted to Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/details/620081/