在C#中打印Unicode字体——Sinhala Unicode字体渲染问题

4
private void button1_Click(object sender, EventArgs e)
    {

        string str = "අම්මා";
        byte[] utf8Bytes = Encoding.UTF8.GetBytes(str);
        String productLine = Encoding.UTF8.GetString(utf8Bytes);

        PrintDocument p = new PrintDocument();

        p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
        {
            e1.Graphics.DrawString(productLine, new Font("Iskoola Pota", 18), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));

        };
        try
        {
            p.Print();
        }
        catch (Exception ex)
        {
            throw new Exception("Exception Occured While Printing", ex);
        }
    }

这段代码可以运行,但我的文本是"අම්මා",但它显示为අ්මමා",请帮助我以正确的方式显示。 "Iskoola Pota" 是 Unicode 字体。


你尝试过直接使用 str 吗?如果这样做会有什么结果? - Fildor
不包含UTF8编码,输出相同。 - Isuru Chanaka
也许这个链接可以帮到你:https://dev59.com/B2025IYBdhLWcg3wIyOf - Fildor
2个回答

0
使用 TextRenderer.DrawText 替代 Graphics.DrawString
TextRenderer.DrawText(e1.Graphics, "අම්මා", font, rectangle, Color.Black);

0

UTF-8 不是 Unicode。当你在源代码中声明字符串时,它就是 Unicode。在你的情况下,去除短元音的变音符号在转换过程中丢失了。但更糟糕的事情可能发生,例如参见c#中的 Unicode 转换

为什么要选择通过 UTF-8 的方式呢?何不使用简单的

e1.Graphics.DrawString(str, ...

另外,您是否检查过您的“School Book”字体是否正常,即能够显示无声符号 ම්,例如在Word中使用该字体?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接