0

So I have this code in python that converts image into base64. And I have this code in C# winforms that collects that base64 and converts it again in image.

Python Code:

import base64

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

print(imgb64encode)

C# Code:

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

Codes on both sides are both working fine if I'm using a small image that consist of less than 4,000 base64. The system in C# suddenly stop working (or suddenly stop accepting data from python based on my understanding) when I'm sending base64 data that consist of more than 5,000 base64.

What do you think might be the problem with this? Why does C# having a problem accepting a huge amount of standard output from python?

Cheska Abarro
  • 45
  • 1
  • 8
  • 1
    What is the end? You are using ReadTpEnd() which will read to the end of the stream and will not wait until the python script is competed. What you need to do is to terminate each transfer with a character not in the transfer or put a byte count at the beginning of each transfer to the c# will know where each transfer completes. – jdweng Dec 24 '20 at 13:56
  • Thank you for the explanation! Based on my understanding on your explanation, the python process are not completed because C# does not wait for it to complete. Is that right? – Cheska Abarro Dec 27 '20 at 10:08
  • Yes. c# ReadToEnd() before python writes all the data to standard output. – jdweng Dec 27 '20 at 10:36
  • I got it now. Thank you so much! – Cheska Abarro Dec 28 '20 at 15:58

1 Answers1

0

I debug my existing code and I found out that the conflict happens in the Standard Error part. So I remove it and replace my `

ReadToEnd()

to

ReadLine()

and it worked perfectly fine. Here's my updated code.

 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);
                }
            }
Cheska Abarro
  • 45
  • 1
  • 8
  • Fix will work as long as python script add a return at end of writing. This is kind of a kludge to add a return at the end of a base 64 encoded string. – jdweng Dec 28 '20 at 16:33