在 C# 僧伽罗语中打印 unicode 字体呈现问题


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字体

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

为什么要通过 UTF-8 呢?一个简单的呢

e1.Graphics.DrawString(str, ...

另外:您是否检查过您的"教科书"字体是否正常,即可以显示无声的 m ම්,例如在 Word 中使用该字体?

使用 TextRenderer.DrawText 而不是 Graphics.DrawString

TextRenderer.DrawText(e1.Graphics, "අම්මා", font, rectangle, Color.Black);

最新更新