最小化Base64

2024-09-30 22:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我有一个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'

请注意,当我只发送32000个或更少的字符串,但base64由98260组成时,代码运行得非常好

有没有办法最小化这个base64

这是我的python代码:

import sys

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

Tags: 字符串代码text图像truevarsyserror
1条回答
网友
1楼 · 发布于 2024-09-30 22:26:44

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脚本以从标准输入读取,而不是从命令行参数读取

相关问题 更多 >