如何使 process.standardinput 位图流在 tesseract 中工作?

How to make process.standardinput bitmap stream work in tesseract?

我正在尝试捕获桌面图像的一部分,然后 运行 使用 tesseract.exe 捕获它,而不必将捕获的图像作为文件写入磁盘。 但是,下面的代码给我一个错误或一个过程不会结束。请帮忙?

Bitmap bmp=capture_and_crop();
       using (Process process = new Process())
                        {                
                            process.StartInfo.FileName = ts5path+"tesseract.exe";
                        process.StartInfo.Arguments = "stdin" + " stdout -l " + language + " --psm " + PSM_setting + " -c preserve_interword_spaces=1";
                                                    process.StartInfo.UseShellExecute = false;
                            process.StartInfo.RedirectStandardInput = true;
                            process.StartInfo.RedirectStandardOutput = true;
                            process.StartInfo.RedirectStandardError = true;
                            process.StartInfo.CreateNoWindow = true;
                            process.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
        
                            process.Start();
                            
                            BinaryWriter writer = new BinaryWriter(process.StandardInput.BaseStream);//line1. I get error here : standardIn has not been redirected, when I move these 2 lines above process.Start().
                            bmp.Save(writer.BaseStream, System.Drawing.Imaging.ImageFormat.Bmp);//line2.
    //If these lines stay here there will be no redirection exception but this process will never end...
        
                            StreamReader reader = process.StandardOutput;
                            string output = reader.ReadToEnd();
                            addtr1.str = output;
                        
                        process.WaitForExit();
                        }
BinaryWriter writer = new BinaryWriter(process.StandardInput.BaseStream);                                        
                    bmp.Save(writer.BaseStream, System.Drawing.Imaging.ImageFormat.Bmp);
    process.StandardInput.BaseStream.Close();

非常感谢 jdweng。 process.StandardInput.BaseStream.Close();在将位图写入标准输入基本流后立即需要关闭流,以便 ReadtoEnd 可以关闭并且程序可以在之后继续。