我需要将 RTF 格式的文本放入 richtextbox 中,我尝试将其与richtextbox.rtf = TextString
参数,但问题是该字符串具有特殊字符,并且 Richtextbox 无法正确显示所有字符串。我正在使用的字符串和代码:
字符串(文本字符串):
╔═══This is only an example, the special characters may change═══╗
C# Code:
String TextString = System.Text.Encoding.UTF8.GetString(TextBytes);
String TextRTF = @"{\rtf1\ansi " + TextString + "}";
richtextbox1.Rtf = TextRTF;
使用此代码,richtextbox 显示“+---这只是一个示例,特殊字符可能会更改---+”,并且在某些情况下显示“??????”。
我怎么解决这个问题?如果我改变\rtf1\ansi
to \rtf1\utf-8
,我没有看到变化。
您可以简单地使用Text
财产:
richTextBox1.Text = "╔═══This is only an example, the special characters may change═══╗";
如果您想使用RTF
财产:
看看这个问题:如何将 unicode 字符串输出为 RTF(使用 C#) https://stackoverflow.com/questions/1368020/how-to-output-unicode-string-to-rtf-using-c
您需要使用类似的方法将特殊字符转换为 rtf 格式:
static string GetRtfUnicodeEscapedString(string s)
{
var sb = new StringBuilder();
foreach (var c in s)
{
if(c == '\\' || c == '{' || c == '}')
sb.Append(@"\" + c);
else if (c <= 0x7f)
sb.Append(c);
else
sb.Append("\\u" + Convert.ToUInt32(c) + "?");
}
return sb.ToString();
}
然后使用:
richtextbox1.Rtf = GetRtfUnicodeEscapedString(TextString);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)