2012-03-09 36 views
6

因爲我最近纔開始使用C#,所以你們都不得不原諒我的無知。我只是有一個關於Windows圖表控件的問題,因爲我遇到了一個相當愚蠢的問題。保存更高分辨率的圖表而不會弄亂外觀

我有一個程序,有幾個報告,包括漂亮的外觀圖表來表示一些數據。不過,我一直使用類似這樣的方式將這些圖表保存到文件中以及用於各種用途:

chart2.SaveImage(savefilename,ChartImageFormat.Png);

我的第一個問題就在於,我不知道如何保存這爲更高的分辨率,不保存之前,首先增加了圖表控件的大小。有一個合理的質量圖像將是很好的。

第二個問題是,當我這樣做增加圖表控件的大小,可用的操作似乎只是能夠增加實際的圖表,而不是標籤或文本的大小。如果我可以手動更改所有這些,這不會成爲問題,這正是我爲條形圖所做的,但有一行我無法弄清楚如何製作更厚:餅圖上的標籤線。我有一個箭頭到它下面的圖片:

http://www.bolinger.ca/chart.png

所以當圖表增加到合理的解決這一行是因爲不增加,以適當的相對大小几乎看不見。我覺得應該有辦法改變它,但不知道它會是什麼。

再次,請原諒我的無知。如果這兩個問題中的任何一個都能解決,那麼我可以輕鬆地知道這些餅圖看起來不錯。謝謝!

回答

1

嘗試設置chart2.RenderTransform = new ScaleTransform(10,10)並保存。這也應該讓你的線條更大。

8

創建/窗體上覆制一個隱藏(可見=假)圖表對象。你甚至可以將其Top和Left屬性設置爲不在表單中。將此控件設置爲非常高的寬度和高度(即2100 x 1500)...填充並格式化爲您的規格。請務必增加字體大小等。然後從隱藏圖表中調用SaveImage()或DrawToBitmap()...

保存此文件時,它基本上具有足夠高的分辨率,可用於大多數文字處理,桌面酒吧,印刷等。例如,2100 1×1500 @ 300 DPI = 7" ×5" 打印...

在您的應用程序,你也可以將其縮小或打印:縮小‘補充’的決議,所以圖像變得更清晰。放大會使圖像模糊或模糊。

我不得不依靠這種技術,因爲它是最穩定的方式來獲得從.NET圖表控件用於打印或保存高分辨率圖...這是一個典型的騙子,但它的工作原理:)

例如:

private void cmdHidden_Click(object sender, EventArgs e) { 
    System.Windows.Forms.DataVisualization.Charting.Title chtTitle = 
     new System.Windows.Forms.DataVisualization.Charting.Title(); 
    System.Drawing.Font chtFont = new System.Drawing.Font("Arial", 42); 
    string[] seriesArray = { "A", "B", "C" }; 
    int[] pointsArray = { 1, 7, 4 }; 

    chart1.Visible = false; 
    chart1.Width = 2100; 
    chart1.Height = 1500; 
    chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Bright; 

    chtTitle.Font = chtFont; 
    chtTitle.Text = "Demographics Comparison"; 
    chart1.Titles.Add(chtTitle); 

    chart1.Series.Clear(); 

    // populate chart  
    for (int i = 0; i < seriesArray.Length; i++) { 
     Series series = chart1.Series.Add(seriesArray[i]); 
     series.Label = seriesArray[i].ToString(); 
     series.Font = new System.Drawing.Font("Arial", 24); 
     series.ShadowOffset = 5; 
     series.Points.Add(pointsArray[i]); 
    } 

    // save from the chart object itself 
    chart1.SaveImage(@"C:\Temp\HiddenChart.png", ChartImageFormat.Png); 

    // save to a bitmap 
    Bitmap bmp = new Bitmap(2100, 1500); 
    chart1.DrawToBitmap(bmp, new Rectangle(0, 0, 2100, 1500)); 
    bmp.Save(@"C:\Temp\HiddenChart2.png"); 
} 
1

這裏有一類我做了做更大的圖表,保存它,然後還原圖。適合我的目的。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using OfficeOpenXml.Drawing; 
using OfficeOpenXml.Drawing.Chart; 
using System.Drawing.Imaging; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.Windows.Forms; 

namespace Simple_Grapher 
{ 
    class saveQualityChartImage 
    { 
     Chart theChart; 
     System.Drawing.Font oldFont1 = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold); 
     System.Drawing.Font oldFont2 = new System.Drawing.Font("Trebuchet MS", 15F, System.Drawing.FontStyle.Bold); 
     System.Drawing.Font oldFont3 = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold); 
     System.Drawing.Font oldLegendFont = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold); 

     int oldLineWidth1; 
     int oldLineWidth2; 
     int oldLineWidth3; 
     int oldLineWidth4; 

     int oldWidth; 
     int oldHeight; 
     public saveQualityChartImage(Chart inputChart) 
     { 
      if (!(inputChart.Series.Count > 0)) 
      { 
       return; 
      } 
      theChart = inputChart; 
      if (inputChart.Titles.Count > 0) 
      { 
       oldFont1 = inputChart.Titles[0].Font; 
      } 
      oldFont2 = inputChart.ChartAreas[0].AxisX.LabelStyle.Font; 
      oldFont3 = inputChart.ChartAreas[0].AxisX.TitleFont; 
      if (theChart.Legends.Count > 0) 
      { 
       oldLegendFont = theChart.Legends["Legend"].Font; 
      } 
      oldLineWidth1 = theChart.ChartAreas[0].AxisX.LineWidth; 
      oldLineWidth2 = theChart.ChartAreas[0].AxisX.MajorTickMark.LineWidth; 
      oldLineWidth3 = theChart.Series[0].BorderWidth; 
      oldLineWidth4 = theChart.ChartAreas[0].AxisY.MajorGrid.LineWidth; 
      oldWidth = theChart.Width; 
      oldHeight = theChart.Height; 

      saveimage(); 
     } 

     public void saveimage() 
     { 
      theChart.Visible = false; 
      System.Drawing.Font chtFont = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold); 
      System.Drawing.Font smallFont = new System.Drawing.Font("Trebuchet MS", 15F, System.Drawing.FontStyle.Bold); 
      if (theChart.Titles.Count > 0) 
      { 
       theChart.Titles[0].Font = chtFont; 
      } 

      theChart.ChartAreas[0].AxisX.TitleFont = chtFont; 
      theChart.ChartAreas[0].AxisX.LineWidth = 3; 
      theChart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 3; 
      theChart.ChartAreas[0].AxisX.LabelStyle.Font = smallFont; 
      theChart.ChartAreas[0].AxisX.MajorTickMark.LineWidth = 3; 

      theChart.ChartAreas[0].AxisY.TitleFont = chtFont; 
      theChart.ChartAreas[0].AxisY.LineWidth = 3; 
      theChart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 3; 
      theChart.ChartAreas[0].AxisY.LabelStyle.Font = smallFont; 
      theChart.ChartAreas[0].AxisY.MajorTickMark.LineWidth = 3; 
      if (theChart.Legends.Count > 0) 
      { 
       theChart.Legends["Legend"].Font = smallFont; 
      } 


      foreach (Series series in theChart.Series) 
      { 
       series.BorderWidth = 3; 

      } 

      theChart.Width = 1800; 
      theChart.Height = 1200; 

      SaveFileDialog save = new SaveFileDialog(); 
      save.DefaultExt = ".png"; 
      if (save.ShowDialog() == DialogResult.OK) 
      { 
       theChart.SaveImage(save.FileName, ChartImageFormat.Png); 
      } 
      resetOldValues(); 

     } 

     private void resetOldValues() 
     { 
      if (theChart.Titles.Count > 0) 
      { 
       theChart.Titles[0].Font = oldFont1; 
      } 

      theChart.ChartAreas[0].AxisX.TitleFont = oldFont3; 
      theChart.ChartAreas[0].AxisX.LineWidth = oldLineWidth1; 
      theChart.ChartAreas[0].AxisX.MajorGrid.LineWidth = oldLineWidth4; 
      theChart.ChartAreas[0].AxisX.LabelStyle.Font = oldFont2; 
      theChart.ChartAreas[0].AxisX.MajorTickMark.LineWidth = oldLineWidth2; 

      theChart.ChartAreas[0].AxisY.TitleFont = oldFont3; 
      theChart.ChartAreas[0].AxisY.LineWidth = oldLineWidth1; 
      theChart.ChartAreas[0].AxisY.MajorGrid.LineWidth = oldLineWidth4; 
      theChart.ChartAreas[0].AxisY.LabelStyle.Font = oldFont2; 
      theChart.ChartAreas[0].AxisY.MajorTickMark.LineWidth = oldLineWidth2; 
      if (theChart.Legends.Count > 0) 
      { 
       theChart.Legends["Legend"].Font = oldLegendFont; 
      } 



      foreach (Series series in theChart.Series) 
      { 
       series.BorderWidth = oldLineWidth3; 

      } 

      theChart.Width = oldWidth; 
      theChart.Height = oldHeight; 
      theChart.Visible = true; 
     } 
    } 
}