问题描述
我试图捕获一部分桌面映像,然后使用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();在将位图写入stdin基本流中以关闭流后立即需要该位,以便ReadtoEnd可以关闭并且程序可以在之后继续运行。