最小化 Base64

Minimize Base64

所以,我有一个 C# 代码,可以将图像转换为 base64,反之亦然。现在,我想将生成的 base64 发送到 python。

这是我现有的代码。

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

            using (Process process = Process.Start(startProcess))
            {
                error = process.StandardError.ReadToEnd();
                testResult = process.StandardOutput.ReadToEnd();
                lblTestOutput.Text = testResult;
                lblError.Text = error;
            }

当我试图将一个小的字符串值发送到 python 时,这段代码工作正常。但是在发送base64值的时候,出现了异常错误。

System.ComponentModel.Win32Exception: 'The filename or extension is too long'

请注意,当我仅发送 32,000 个或更少的字符串但 base64 恰好包含 98,260 个时,代码工作正常。

有没有办法最小化这个 base64?

这是我的 python 代码:

import sys

inputFromC = sys.stdin
print("Python Recevied: ", inputFromC)

Windows 中命令 + 参数的最大长度为 32767 个字符 (link)。这与您所看到的一致。

我建议改为通过进程的标准输入发送图像。类似于:

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

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

    error = process.StandardError.ReadToEnd();
    testResult = process.StandardOutput.ReadToEnd();
    lblTestOutput.Text = testResult;
    lblError.Text = error;
}

显然,修改您的 Python 脚本以从标准输入而不是命令行参数读取。