Saving higher resolution charts without messing up the appearance

后端 未结 3 1769
时光取名叫无心
时光取名叫无心 2020-12-30 10:57

you\'ll all have to excuse my ignorance as I have only recently started working with C#. I just have a question about the windows chart control, as I\'m encountering a rathe

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 11:26

    Create/Duplicate a hidden (Visible = false) chart object on the form. You can even set its Top and Left properties to be off of the form. Set this control to a very high Width and Height (i.e., 2100 x 1500)... Populate and format it to your specifications. Be sure to increase the font sizes, etc. Then call SaveImage() or DrawToBitmap() from the hidden chart...

    When you save this file, it will essentially be high enough resolution for most word processing, desktop pubs, printing, etc. For example, 2100 x 1500 @ 300 dpi = 7" x 5" for printing...

    In your application, you can also scale it down or print it: scaling down "adds" resolution, so the image becomes sharper. Scaling up makes an image blurry or fuzzy.

    I have had to rely on this technique, as it is the most consistent way to get high-res charts from the .Net chart control for printing or saving... It is a classic cheat, but it works :)

    For example:

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

提交回复
热议问题