簡體   English   中英

使用mathematica在C#中繪圖

[英]Plotting in C# using mathematica

請幫助我繪制從 C# 發送到mathematica kernel 的數據。 我想從 C# 和 plot 光譜數據編寫數學代碼。 我想我已經添加了必要的程序集和引用。 我的 C# 代碼如下。

public void button2_Click(object sender, System.EventArgs e)
{
       double baseline;
       int indexOfPeak;
       int minimumIndicesBetweenPeaks;
       int numberOfPixels; 
       double[] spectrum;
       int startingIndex;
       if (spectrometerIndex == -1)
          return; 
       numberOfPixels = wrapper.getNumberOfPixels(spectrometerIndex);
       wrapper.setIntegrationTime(spectrometerIndex, 500000);
       wrapper.setBoxcarWidth(spectrometerIndex, 10);
       wrapper.setCorrectForElectricalDark(spectrometerIndex, 1);
       spectrum = (double[])wrapper.getSpectrum(spectrometerIndex);
       for (int index = 0; index < numberOfPixels; ++index)
       {
           listBox2.Items.Add("pixel[" + index + "] = " +
           spectrum[index]);               
       }
       MathKernel mathKernel = new MathKernel();
       mathkernel.ListPlot[spectrum];
    }            
}

這里有幾個方法,基於這里的一篇文章

您可能需要為 Mathematica 適當地格式化您的spectrum數據,例如

string data = "1.234, 2.345, 3.456";

對於 GIF 格式

MathKernel mathKernel = new MathKernel();
mathKernel.CaptureGraphics = true;
mathKernel.GraphicsFormat = "GIF";
mathKernel.Compute("Show[ListPlot[{" + data + "}]]");
mathKernel.Graphics[0].Save("C:\\Temp\\plot.gif", System.Drawing.Imaging.ImageFormat.Gif);

對於增強的元文件(可縮放圖形)

MathKernel mathKernel  = new MathKernel();
mathKernel.Compute("ExportString[ListPlot[{" + data + "}], {\"Base64\", \"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(mathKernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\plot.emf", decodedBytes);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM