【问题标题】:Saving higher resolution charts without messing up the appearance保存更高分辨率的图表而不会弄乱外观
【发布时间】:2012-03-27 08:08:51
【问题描述】:

你们都必须原谅我的无知,因为我最近才开始使用 C#。我只是有一个关于 windows 图表控件的问题,因为我遇到了一个相当愚蠢的问题。

我有一个程序,它有一些报告,其中包括漂亮的窗口图表来表示一些数据。但是,我也一直将这些图表保存到文件中以用于各种用途,只需使用以下内容:

chart2.SaveImage(savefilename, ChartImageFormat.Png);

我的第一个问题在于,如果在保存之前不先增加图表控件的大小,我不确定如何将其保存为更高分辨率。有一个质量合理的图像会很好。

第二个问题是当我增加图表控件的大小时,可用的操作似乎只能增加实际图表的大小,而不是标签或文本。如果我可以手动更改所有这些,这将不是问题,这是我为条形图所做的,但有一条线我不知道如何使更粗:饼图上的标签线.我在下图中为它画了一个箭头:

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

因此,当图表增加到合理的分辨率时,由于没有增加到适当的相对大小,这条线几乎不可见。我觉得应该有办法改变它,但无法弄清楚它会是什么。

再次,原谅我的无知。如果这两个问题中的任何一个都可以解决,那么知道这些饼图看起来不错,我就可以高枕无忧了。谢谢!

【问题讨论】:

    标签: c# charts resize controls resolution


    【解决方案1】:

    在表单上创建/复制一个隐藏(可见 = false)图表对象。您甚至可以将其 Top 和 Left 属性设置为脱离表单。将此控件设置为非常高的宽度和高度(即 2100 x 1500)...根据您的规范填充和格式化。一定要增加字体大小等。然后从隐藏图表中调用 SaveImage() 或 DrawToBitmap()...

    当您保存此文件时,它的分辨率对于大多数文字处理、桌面发布、打印等来说基本上是足够高的。例如,2100 x 1500 @ 300 dpi = 7" x 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");
    }
    

    【讨论】:

    【解决方案2】:

    这是我制作的一个类,用于制作更大的图表,保存它,然后恢复图表。很适合我的目的。

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

    【讨论】:

      【解决方案3】:

      尝试设置chart2.RenderTransform = new ScaleTransform(10,10) 并保存。这也应该使你的线条更大。

      【讨论】:

        猜你喜欢
        • 2011-04-04
        • 2013-04-11
        • 1970-01-01
        • 2016-12-18
        • 2014-10-03
        • 2015-10-20
        • 1970-01-01
        • 2018-04-27
        相关资源
        最近更新 更多