2012-03-09 13 views
6

, todos tendrán que disculpar mi ignorancia, ya que recientemente comencé a trabajar con C#. Solo tengo una pregunta sobre el control gráfico de Windows, ya que estoy encontrando un problema bastante tonto.Guardando gráficos de mayor resolución sin estropear la apariencia

Tengo un programa que tiene algunos informes que incluyen agradables gráficos de Windows para representar algunos datos. Sin embargo, he estado guardando estos cuadros en archivos también para varios usos, simplemente usando algo como esto:

chart2.SaveImage (savefilename, ChartImageFormat.Png);

Mi primer problema radica en el hecho de que no estoy seguro de cómo guardar esto como una resolución más alta sin antes aumentar el tamaño del control de gráfico antes de guardar. Sería bueno tener una imagen de calidad razonable.

El segundo problema es que cuando aumento el tamaño del control de gráfico, las operaciones disponibles solo parecen ser capaces de aumentar el tamaño del gráfico real, no las etiquetas o el texto. Esto no sería un problema si pudiera cambiar todos estos manualmente, que es lo que he hecho para un gráfico de barras, pero hay una línea que no puedo entender cómo hacer más gruesa: las líneas de etiqueta en el gráfico circular . He dibujado una flecha para que en la siguiente imagen:

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

Así que cuando el gráfico se incrementa a una resolución razonable esta línea es casi invisible debido a que no aumenta a un tamaño relativo adecuado. Siento que debería haber una forma de cambiarlo, pero no puedo descifrar cuál sería.

Nuevamente, disculpe mi ignorancia. Si cualquiera de estos dos problemas pudiera resolverse, podría descansar tranquilo sabiendo que estos gráficos circulares parecen decentes. ¡Gracias!

Respuesta

1

Pruebe la configuración chart2.RenderTransform = new ScaleTransform(10,10) y guárdela. Esto también debería agrandar tus líneas.

8

Crear/Duplicar un oculto (Visible = falso) objeto de gráfico en el formulario. Incluso puede configurar sus propiedades Superior e Izquierda para estar fuera del formulario. Establezca este control en un ancho y una altura muy altos (es decir, 2100 x 1500) ... Rellene y formatee según sus especificaciones. Asegúrese de aumentar los tamaños de fuente, etc. Luego, llame a SaveImage() o DrawToBitmap() desde el gráfico oculto ...

Al guardar este archivo, será esencialmente lo suficientemente alta resolución para la mayoría de los procesadores de texto, pubs de escritorio , impresión, etc. Por ejemplo, 2100 x 1500 @ 300 ppp = 7 "x 5" para imprimir ...

En su aplicación, también puede reducirlo o imprimirlo: reduciendo la resolución de "suma", por lo que la imagen se vuelve más nítida. Escalar hace que una imagen sea borrosa o borrosa.

He tenido que confiar en esta técnica, ya que es la forma más consistente de obtener gráficos de alta resolución desde el control de gráfico .Net para imprimir o guardar ... Es un truco clásico, pero funciona :)

Por ejemplo:

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

Aquí es una clase que hice para hacer una gráfica grande, guardarlo, a continuación, restaurar la gráfica. Funciona bien para mi mis propósitos.

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; 
     } 
    } 
} 
Cuestiones relacionadas