保存更高分辨率的图表而不会弄乱外观 [英] Saving higher resolution charts without messing up the appearance

查看:18
本文介绍了保存更高分辨率的图表而不会弄乱外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

chart2.SaveImage(savefilename, ChartImageFormat.Png);

我的第一个问题在于,我不知道如何将其保存为更高的分辨率,而无需在保存之前先增加图表控件的大小.拥有合理质量的图像会很好.

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

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

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

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

解决方案

在表单上创建/复制隐藏(Visible = 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 =新 System.Windows.Forms.DataVisualization.Charting.Title();System.Drawing.Font chtFont = new System.Drawing.Font("Arial", 42);string[] seriesArray = { "A", "B", "C" };int[] 点数组 = { 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 = "人口统计比较";chart1.Titles.Add(chtTitle);chart1.Series.Clear();//填充图表for (int i = 0; i < seriesArray.Length; i++) {系列 series = chart1.Series.Add(seriesArray[i]);series.Label = seriesArray[i].ToString();series.Font = new System.Drawing.Font("Arial", 24);系列.ShadowOffset = 5;series.Points.Add(pointsArray[i]);}//从图表对象本身保存chart1.SaveImage(@"C:TempHiddenChart.png", ChartImageFormat.Png);//保存到位图位图 bmp = 新位图 (2100, 1500);chart1.DrawToBitmap(bmp, new Rectangle(0, 0, 2100, 1500));bmp.Save(@"C:TempHiddenChart2.png");}

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 rather dumb problem.

I have a program which has a few reports that include nice looking windows Charts to represent some data. However, I have been saving these charts to files as well for various uses, by just using something like this:

chart2.SaveImage(savefilename, ChartImageFormat.Png);

My first problem lies in the fact that I am not sure how to save this as a higher resolution without first increasing the size of the chart control before saving. It would be nice to have an image of reasonable quality.

The second problem is when I do increase the size of the chart control, the available operations only seem to be able to increase the size of the actual chart, not the labels or the text. This wouldn't be a problem if I could change all of these manually, which is what I have done for a bar chart, but there is one line I can't figure out how to make thicker: the label lines on the pie chart. I have drawn an arrow to it in the following image:

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

So when the chart is increased to a reasonable resolution this line is nearly invisible due to not increasing to an appropriate relative size. I feel like there should be a way to change it, but can't figure out what it would be.

Again, excuse my ignorance. If either one of these two problems could be solved then I could rest easy knowing that these pie charts look decent. Thanks!

解决方案

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:TempHiddenChart.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:TempHiddenChart2.png");
}

这篇关于保存更高分辨率的图表而不会弄乱外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆