2017-05-23 8 views
0

折れ線グラフを作成して画像として保存し、画像を電子メールに添付したいと考えました。 これは実装ですが、保存された画像にはグラフがありません。保存C#の線グラフ

public void CreateLineGraph(string savePath) 
     { 
      var chart1 = new Chart 
      { 
       Height = 300, 
       Width = 500 
      }; 
      var series = new Series("Export"); 
      IEnumerable<string> xValue = DaysInAWeek1(); 
      IEnumerable<int> yValue = RangeValue1(); 
      series.Points.DataBindXY(xValue, yValue); 
      series.Points.AddXY(10,20); 
      chart1.Series.Add(series); 
      series.ChartType = SeriesChartType.Line; 

      chart1.SaveImage(savePath, ChartImageFormat.Png); 
     } 
     private IEnumerable<string> DaysInAWeek1() 
     { 
      IEnumerable<string> m_oEnum = new string[] { "1", "2", "3" }; 
      return m_oEnum; 
     } 

     private IEnumerable<int> RangeValue1() 
     { 
      IEnumerable<int> m_oEnum = new int[] { 0, 5, 10 }; 
      return m_oEnum; 
     } 

画像: enter image description here

+1

クラスのどのような種類のチャートおよびシリーズですか? –

+0

あなたは、ChartAreaを追加することを잊既にされている! – TaW

答えて

-1

あなたの画面からスクリーンショットを取り、それを保存することができます! あなたが望む画像(グラフを含む画像の部分)をカットします。 マイコードはこれです:

IDataObject data; 
       Image bmap; 
       //---copy the image to the Clipboard--- 
       SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0); 
       //---retrieve the image from Clipboard and convert it 
       // to the bitmap format--- 
       data = Clipboard.GetDataObject(); 

       if (data.GetDataPresent(typeof(System.Drawing.Bitmap))) 
       { 
        bmap = 
         ((Image)(data.GetData(typeof(
          System.Drawing.Bitmap)))); 

        if (!Directory.Exists("Your Directory that u wanna to save pic on it!")) 
        { 
         Directory.CreateDirectory("Directory path"); 
        } 
        bmap.Save("The Directory u wanna to save and the filename", System.Drawing.Imaging.ImageFormat.Jpeg); 
       } 

は、フォームの上に、このAPIを追加することを忘れないでください:

[System.Runtime.InteropServices.DllImport(
     "user32", EntryPoint = "SendMessageA")] 
    static extern int SendMessage(
    int hwnd, int Msg, int wParam, 
    [MarshalAs(UnmanagedType.AsAny)] object lParam); 
+0

グラフの私の問題。それは画像の無地の白だけで、グラフは表示されません – Jen143

+0

Chartは良いSaveメソッドを持っています。 – TaW