gpt4 book ai didi

c# - RichTextBox 在 Azure 上抛出 OutOfMemory

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:34 25 4
gpt4 key购买 nike

我正在使用 RichTextBox 将 RTF 中的字符串转换为纯文本,使用这段代码:

private string ConvertToText(string rtf)
{
if (string.IsNullOrWhiteSpace(rtf)) return string.Empty;

if (!rtf.Contains("{\\rtf")) return rtf.Trim();

using (var helper = new System.Windows.Forms.RichTextBox())
{
helper.Rtf = rtf;
var plainText = helper.Text;

if (string.IsNullOrWhiteSpace(plainText)) return string.Empty;

return "<< Rule in Rich Text Format converted to Plain Text >>\n\n"
+ plainText
+ "\n\n<< Rule in Rich Text Format converted to Plain Text >>";
}
}

它在所有开发人员计算机上运行正常,但在部署到 Azure 网站时不起作用:

{
"$id" :"1",
"Message" :"An error has occurred.",
"ExceptionMessage":"Out of memory.",
"ExceptionType" :"System.OutOfMemoryException",
"StackTrace" :"
at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)\r\n
at System.Drawing.Font.GetHeight()\r\n
at System.Drawing.Font.get_Height()\r\n
at System.Windows.Forms.Control.get_FontHeight()\r\n
at System.Windows.Forms.TextBoxBase.get_PreferredHeight()\r\n
at System.Windows.Forms.TextBoxBase.AdjustHeight(Boolean returnIfAnchored)\r\n
at System.Windows.Forms.TextBoxBase.set_Multiline(Boolean value)\r\n
at System.Windows.Forms.RichTextBox.set_Multiline(Boolean value)\r\n
at System.Windows.Forms.RichTextBox..ctor()\r\n
at ... "
}

无论RTF 字符串的大小,都会发生这种情况。我已经在使用 System.Windows.Forms 引用的“复制到本地”属性。有人有处理 RTF 和 Azure 的经验吗?是否有其他方法将 RTF 转换为纯文本?

最佳答案

这是此 C++ forum link 的改编 C# 版本@Gusman 在评论中建议:

public static string ConvertToText(string rtf)
{
bool slash = false; //indicates if backslash followed by the space
bool figure_opened = false; //indicates if opening figure brace followed by the space
bool figure_closed = false; //indicates if closing brace followed by the space
bool first_space = false; //the else spaces are in plain text and must be included to the result

if (rtf.Length < 4) return string.Empty;

int i = 0;
i = rtf.IndexOf("\\pard");
if (i < 1) return "";

var builder = new StringBuilder();
for (int j = i; j < rtf.Length - 1; j++)
{
char ch = rtf[j];
char nextCh = rtf[j + 1];

if (ch == '\\' && nextCh == 'p') // appends \n if \pard, except for first
{
if (j > i && j < rtf.Length - 4)
{
string fiveChars = rtf.Substring(j, 5);
if (fiveChars.Equals("\\pard"))
{
builder.Append("\n");
}
}
}

if (ch == '\\' && nextCh == 'u') // to deal correctly with special characters
{
string fourChars = rtf.Substring(j + 2, 4);
string digits = new string(fourChars.TakeWhile(char.IsDigit).ToArray());
char specialChar = (char)int.Parse(digits);
builder.Append(specialChar);
j += digits.Length + 5;
continue;
}

if (ch == '\\' && nextCh == '{') // if the text contains symbol '{'
{
slash = false;
figure_opened = false;
figure_closed = false;
first_space = false;
builder.Append('{');
j++;
continue;
}
else if (ch == '\\' && nextCh == '}') // if the text contains symbol '}'
{
slash = false;
figure_opened = false;
figure_closed = false;
first_space = false;
builder.Append('}');
j++;
continue;
}
else if (ch == '\\' && nextCh == '\\') // if the text contains symbol '\'
{
slash = false;
figure_opened = false;
figure_closed = false;
first_space = false;
builder.Append('\\');
j++;
continue;
}
else if (ch == '\\') // we are looking at the backslash
{
first_space = true;
slash = true;
}
else if (ch == '{')
{
first_space = true;
figure_opened = true;
}
else if (ch == '}')
{
first_space = true;
figure_closed = true;
}
else if (ch == ' ')
{
slash = false;
figure_opened = false;
figure_closed = false;
}

if (!slash && !figure_opened && !figure_closed)
{
if (!first_space)
{
builder.Append(ch);
}
else
{
first_space = false;
}
}
}
return builder.ToString();
}

它有效! =D

关于c# - RichTextBox 在 Azure 上抛出 OutOfMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com