0

So, I have a code in C# that converts the Image to base64 and vice versa. Now, I want to send the generated base64 to python.

Here's my existing code.

            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;
            }

This code is working just fine when I'm trying to send a small value of string to python. But when it comes in sending base64 value, an exception error appeared.

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

Take note that the code is working perfectly fine when I'm sending only 32,000 string and less but base64 consist of exactly 98,260.

Is there a way to minimize this base64?

This is my python code:

import sys

inputFromC = sys.stdin
print("Python Recevied: ", inputFromC)
Cheska Abarro
  • 45
  • 1
  • 8
  • 3
    If you minimized it, it would not be base64 any more. Sending the original binary would already be "minimized" compared to base64. – zvone Dec 22 '20 at 16:32
  • You may want to reconsider how you go about accepting images to your Python program given this constraint: https://stackoverflow.com/questions/2381241/what-is-the-subprocess-popen-max-length-of-the-args-parameter. – kingkupps Dec 22 '20 at 16:35

1 Answers1

2

The maximum length for a command + arguments in Windows is 32767 characters (link). This is consistent with that you're seeing.

I recommend sending the image over the process's standard input instead. Something like:

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;
}

Obviously, modify your Python script to read from standard input, rather than a command-line argument.

canton7
  • 37,633
  • 3
  • 64
  • 77
  • Or write the base 64 to a temporary file, and then tell your Python code to read that file. – Frank Yellin Dec 22 '20 at 16:47
  • @FrankYellin Sure, but bear in mind you need to clean up the temporary file, even if e.g. the machine is shut down while the C#/Python code is still executing. – canton7 Dec 22 '20 at 16:47
  • The error disappeared. You're brilliant! Thank you! Can I also know how to get python receive standard input? I'll add my existing python code in my question. it will so much great help. – Cheska Abarro Dec 22 '20 at 17:43
  • `sys.stdin.read()`, says Google. I don't do Python. – canton7 Dec 22 '20 at 17:59