2017-06-06 147 views
-3

我想通过单击保存按钮将我绘制的图保存为JPEG图像。请给我帮忙解决这个问题。这是我绘制的grapg的样子。 my plotted graph area如何将绘图图保存为C#格式的JPEG图像

这是我用来绘制图形的代码。

private void Output_Load(object sender, EventArgs e) 
     { 

      List<Graph> ObservingData = new List<Graph>(); // List to store all available Graph objects from the CSV 

      // Loops through each lines in the CSV 
      foreach (string line in System.IO.File.ReadAllLines(pathToCsv).Skip(1)) // .Skip(1) is for skipping header 
      { 
       // here line stands for each line in the csv file 

       string[] InCsvLine = line.Split(','); 

       // creating an object of type Graph based on the each csv line 

       Graph Inst1 = new Graph(); 


       Inst1.AvI = double.Parse(InCsvLine[1]); 
       Inst1.AvE = double.Parse(InCsvLine[2]); 

       chart1.Series["Speed"].YAxisType = AxisType.Primary; 
       chart1.Series["Speed"].Points.AddXY(Inst1.Date.AvI, Inst1.AvE); 
       chart1.Series["Speed"].ChartType = SeriesChartType.FastLine; 

      } 
     } 

这是我的.csv文件数据的一部分,如下所示;

Name,AvI,AvE,Test 
Amal,3.28000,100,TRUE 
Kamal,3.30000,150,FALSE 
Ann,3.32000,200,FALSE 
Jery,3.34000,220,FALSE 
TaW,3.39000,130,FALSE 
Nane,3.40000,125,TRUE 
Petter,3.42000,300,TRUE 
Sam,3.46000,265,TRUE 
Daniyel,3.50000,245,TRUE 
Don,3.62000,146,FALSE 
Zip,3.64000,201,FALSE 
Sera,3.68000,300,FALSE 
Perera,3.70000,200,TRUE 
Dam,3.90000,170,TRUE 

回答

0

以下是使用SaveFileDialog并将图表输出为PNG图像的示例。

private void buttonSave_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string path; 
     SaveFileDialog sfd = new SaveFileDialog(); 
     sfd.Filter = "Png Image (.png)|*.png"; 

     if (sfd.ShowDialog() == DialogResult.OK) 
     { 
      path = sfd.FileName; 

      if (!string.IsNullOrEmpty(path)) 
      { 
       chart1.SaveImage(path, ChartImageFormat.Png); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

,非常感谢你的帮助。它正在工作。 – SNP

1

您是否尝试过在图表控件上使用SaveImage方法?

public class Chart : Control, ISupportInitialize, IDisposable 
    { 

    /// <summary>Saves an image to the specified file.</summary> 
    /// <param name="imageFileName">The name of the file in which image is saved to.</param> 
    /// <param name="format">The image format.</param> 
    public void SaveImage(string imageFileName, ImageFormat format) 

使用范例

speedChart.SaveImage("speedChart", ImageFormat.Jpeg); 
+0

我是一个新的初学者,所以我没有任何关于我多么想开始保存JPEG image.I加入我绘制的图形代码此question.please给我任何帮助的想法。 – SNP

0

我发现this.This的解决方案是为工作代码。

private void btnSave_Click(object sender, EventArgs e) 
     { 
      SaveFileDialog dlg = new SaveFileDialog(); 
      if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
       this.chart1.SaveImage(dlg.FileName, ChartImageFormat.Jpeg); 
      MessageBox.Show("Chart details Successful saved as jpeg image"); 
     } 
相关问题