将 Python 中的大量字符串传递给 C#

Pass huge amount of string from Python to C#

所以我在 python 中有这段代码可以将图像转换为 base64。我在 C# winforms 中有这段代码,它收集 base64 并在图像中再次转换它。

Python代码:

import base64

imgtob64 = open('Resources/yudodis.jpg', 'rb')
imgtob64_read = imgtob64.read()
imgb64encode = base64.encodebytes(imgtob64_read)

print(imgb64encode)

C#代码:

string TestPath3 = @"C:\Users\chesk\Desktop\imagetobase64.py";
string b64stringPython;
string error;

            var startProcess = new ProcessStartInfo
            {
                FileName = pythonInterpreter, Arguments = string.Format($"\"{TestPath3}\""),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
            };
         

            using (Process process = Process.Start(startProcess))
            {
                try
                {
                    error = process.StandardError.ReadToEnd();
                    b64stringPython = process.StandardOutput.ReadToEnd();

                    lblTestOutput.Text = b64stringPython;
                    lblError.Text = error;
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }

如果我使用包含少于 4,000 个 base64 的小图像,两边的代码都可以正常工作。当我发送包含超过 5,000 个 base64.

的 base64 数据时,C# 中的系统突然停止工作(或突然停止接受来自 python 的数据,根据我的理解)

您认为这可能是什么问题?为什么 C# 在接受来自 python 的大量标准输出时遇到问题?

我调试现有代码,发现冲突发生在标准错误部分。所以我删除它并替换我的`

ReadToEnd()

ReadLine()

而且效果非常好。这是我更新后的代码。

 using (Process process = Process.Start(startProcess))
            {
                try
                {
                    process.StandardInput.WriteLine(b64stringCSharp);
                    process.StandardInput.Close();

                    
                    //error = process.StandardError.ReadToEnd();
                    b64stringPython = process.StandardOutput.ReadLine();
                    
                    //lblTestOutput.Text = b64stringPython;
                    //lblError.Text = error;
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }