我正在尝试捕获部分桌面映像,然后使用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。过程StandardInput.BaseStream.Close((;在位图被写入stdin基流后立即需要关闭流,以便ReadtoEnd可以关闭,程序可以在之后继续运行。