我使用GnuPlot从c#应用程序。我想读取GnuPlot PNG输出直接从标准输出,而不是保存到一个文件,然后读取它。我的代码现在看起来是这样的:
string Path = @"C:Program Filesgnuplotbingnuplot.exe";
Process GnuplotProcess = new Process();
GnuplotProcess.StartInfo.FileName = Path;
GnuplotProcess.StartInfo.UseShellExecute = false;
GnuplotProcess.StartInfo.RedirectStandardInput = true;
GnuplotProcess.StartInfo.RedirectStandardOutput = true;
GnuplotProcess.Start();
StreamWriter SW = GnuplotProcess.StandardInput;
StreamReader SR = GnuplotProcess.StandardOutput;
SW.WriteLine("set terminal pngcairo size 300,200");
foreach (LoadCaseOutput LCO in LoadCases)
{
foreach (LoadCaseOutput.MemberOutput MO in LCO.Members)
{
SW.WriteLine("plot " + MO.GenerateAFEquation(P));
MO.AFImage = Image.FromStream(SR.BaseStream);
}
}
SW.WriteLine("exit");
GnuplotProcess.Close();
现在这个似乎在Image.FromStream()行停止了。出了什么问题?
update(我已经更新了我的代码以反映下面的注释)
似乎问题是当"退出"命令被发送到gnuPlot。如果没有发送给gnuPlot的退出命令,程序将等待。
我以您的示例为例,并且能够通过在执行树中向上移动gnuPlot exit命令来完成程序。
string Path = @"z:toolsgnuplotbingnuplot.exe";
Process GnuplotProcess = new Process();
GnuplotProcess.StartInfo.FileName = Path;
GnuplotProcess.StartInfo.UseShellExecute = false;
GnuplotProcess.StartInfo.RedirectStandardInput = true;
GnuplotProcess.StartInfo.RedirectStandardOutput = true;
GnuplotProcess.Start();
StreamWriter SW = GnuplotProcess.StandardInput;
StreamReader SR = GnuplotProcess.StandardOutput;
SW.WriteLine("set terminal pngcairo size 300,200");
SW.WriteLine("plot f(x) = sin(x*a), a = .2, f(x), a = .4, f(x)");
SW.WriteLine("exit");
Image png = Image.FromStream(SR.BaseStream);
png.Save(@"z:toolstry3a.png");
GnuplotProcess.Close();
这正确地生成了一个PNG文件。为了进行测试,我尝试在发送退出命令之前从流中读取数据。程序等待FromStream调用。
马特问题不在于阅读,否则你会得到一个异常,用这个替换阅读位:
Image.FromStream(oFileStream, false, true)
一旦接收到第一个字节,它将验证图像,并从那里开始。