2013-03-29 19 views
0

我正在使用來自C#應用程序的GnuPlot。我想直接從標準輸出讀取GnuPlot PNG輸出,而不是保存到文件然後閱讀。我的代碼看起來像現在這種權利:在C#中讀取GnuPlot輸出到圖像中#

string Path = @"C:\Program Files\gnuplot\bin\gnuplot.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()線來搪塞。出了什麼問題?

+0

我猜GnuPlot StandardOutput實際上是不輸出圖像。如果將SR.BaseStream重定向到文件會發生什麼情況。你能把它看作一個圖像嗎?根據MSDN,隱含的消息是StandardOutput是文本。 – tatmanblue

+0

如果是停頓是因爲文件結尾沒有被髮送信號。 – Marco

回答

1

更新(我已經更新了我的代碼,以反映我下面的評論)

它出現的問題是,當「退出」命令發送到GNUPLOT。如果沒有將退出命令發送到gnuPlot,程序將等待。

我接受了你的例子,並能夠通過在執行樹中移動gnuPlot exit命令來完成程序。

 string Path = @"z:\tools\gnuplot\bin\gnuplot.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:\tools\try3a.png"); 

     GnuplotProcess.Close(); 

這確實生成了一個PNG文件。對於測試,我在發送exit命令之前嘗試從流中讀取數據。程序等待FromStream調用。

馬特

+0

好吧,我不想退出GnuPlot,因爲它需要一秒鐘才能啓動,我想通過一堆方程式重複此操作。就我所期望的而言,當我用上面代碼中硬編碼的設置運行GnuPlot時,我直接得到一個PNG圖像到標準輸出(這是一串大的ASCII碼,但它以PNG開頭,假設它是一個有效的PNG圖像)。並且我在**之前在控制檯上獲得輸出**我輸入退出命令 – ian93

+0

我修復了我的程序,並且確實收到了StandardOutput(我看到我錯過了代碼中的一行)。但是,我仍然堅持退出命令的位置。我在嘗試閱讀StandardOutput之前需要它,否則它會掛起。但是,結果並不是我可以在任何圖像查看器中加載的PNG。不知道那是怎麼回事。 (同樣可能是我的代碼沒有正確保存輸出) – tatmanblue

+0

我的代碼稍微玩了一會之後,我能夠將流保存到png文件中,該文件的顯示正確。爲了測試,我在嘗試讀取流之後將出口移至,並且程序掛起。在讀取流之前,確實看起來您需要該退出命令。 – tatmanblue

0

的問題是不是與閱讀,否則你會得到一個例外,這個替換讀位:

Image.FromStream(oFileStream, false, true) 

因爲它接收到的第一個字節,會盡快確認的形象和你去那裏。