使用C#的动态图形(要保存为PNG格式) [英] Dynamic graphs using C# (To be saved as a PNG Format)

查看:311
本文介绍了使用C#的动态图形(要保存为PNG格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须即时生成动态图形,将其保存为PNG图像格式(透明背景)以嵌入到PDF中。



问题是,图形将不会显示在页面上的控件中(在WebForm中没有图表控件),并且应该从基础信息生成,并应在创建PDF时嵌入。



PDF和PDF生成的库全部到位。



我想知道如何生成没有图表控件的动态图形(条形图,饼图)3D或简单图形,并且能够将其保存为具有透明背景的图像(.PNG)。



原因是,PDF模板有许多图层阴影,因此图形图像周围的背景应该是透明的,类似于下面的图像。 b
$ b



我曾尝试生成类似于下面示例的动态条形图链接,但图像看起来并不大有希望的



任何有关功能的帮助深表谢意。




更新日期:2016年3月16日星期三



在TaW的回答和更新中,我已经能够绘制出所需的图形,下面是对我提出的同一问题的回答的更新



添加对 System.Windows.Forms System.Windows.Forms.DataVisualization



添加下面的命名空间

  using System.Drawing; 
使用System.Windows.Forms;
使用System.Windows.Forms.DataVisualization.Charting;

下面的方法会给你带有平滑边缘的饼图,如图所示



 图表图表=新图表(); 
ChartArea CA = chart.ChartAreas.Add(A1);
Series S1 = chart.Series.Add(S1);
S1.ChartType = SeriesChartType.Pie;

S1.Points.AddXY(1,17);
S1.Points.AddXY(2,27);
S1.Points.AddXY(3,7);
S1.Points.AddXY(4,49);

chart.BackColor = Color.Fuchsia;
CA.BackColor = chart.BackColor;
CA.Area3DStyle.Enable3D = true;
S1.Points [2] [Exploded] =true;
位图bmp =新位图(chart.Width,chart.Height);
chart.AntiAliasing = AntiAliasingStyles.None;
chart.DrawToBitmap(bmp,chart.ClientRectangle);
bmp.MakeTransparent(chart.BackColor);
bmp.Save(yourFileName,ImageFormat.Png);


I have to generate dynamic graphs on the fly, save it as a PNG image format (Transparent background) to be embedded on to a PDF.

The problem is, the graph will not be displayed on a control in the page (without a chart control in the WebForm) and should be generated from an underlying information and should be embedded while creating the PDF.

The libraries for the PDFs and PDF generation is all in place.

I would like to know how to generate a dynamic graph (Bar graph, Pie Graph) 3D or Simple graph without a chart control in place and be able to save it as an image with transparent background (.PNG).

The reason being, the PDF template has numerous layer shading, hence the background around the graph image should be transparent, something similar to the below image.

I have tried generating a dynamic bar graph similar to the example in the below link but the Image doesn't look promising

https://2leggedspider.wordpress.com/2004/11/21/generating-a-dynamic-bar-graph-using-aspnet-and-c/

Any help regarding the functionality is deeply appreciated. Thank you in advance.


Update: Wednesday 16th March 2016

With TaW's answer and updates I have been able to draw the required graph and below is the update with answer to the same question I asked

Add references to System.Windows.Forms and System.Windows.Forms.DataVisualization

Add below namespaces

using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

And below method would give you the Pie Chart with smooth edges as shown in the picture

void Create3DPieChart()
        {
            /* Utilize the Chart class available in System.Windows.Forms.DataVisualization.Charting */
            Chart chart = new Chart();
            /* Add a chart area to the chart */
            ChartArea CA = chart.ChartAreas.Add("A1");
            /* Add data series to the chart and specify the type of Series */
            Series S1 = chart.Series.Add("S1");
            S1.ChartType = SeriesChartType.Pie;

            /* Assign points for the Series */
            S1.Points.AddXY(1, 17);
            S1.Points.AddXY(2, 27);
            S1.Points.AddXY(3, 7);
            S1.Points.AddXY(4, 49);

            /* Set chart color and other settings as required */
            chart.BackColor = Color.Transparent;
            CA.BackColor = chart.BackColor;
            CA.Area3DStyle.Enable3D = true;
            S1.Points[2]["Exploded"] = "true";

            /*Assign AntiAliasing to Graphics style for smooth edges*/
            chart.AntiAliasing = AntiAliasingStyles.Graphics;

            /* Set the image path and save the image as PNG format*/
            string imageNameAndPath = string.Concat(Application.StartupPath.Remove(Application.StartupPath.IndexOf("\\bin\\Debug")), 
                                        "/TempImages/Image", DateTime.Now.ToString("ddMMyyyyhhmmss") + ".png");
            chart.SaveImage(imageNameAndPath, ChartImageFormat.Png);
        }

TaW. Please correct me if I'm wrong.

解决方案

Unfortunately the chart won't draw any transparency directly, so we need to workaround..

If you want to create a PNG with transparent background here is what you can do:

chart1.BackColor = Color.Fuchsia;
chart1.ChartAreas[0].BackColor = chart1.BackColor;
Bitmap bmp = new Bitmap( chart1.ClientSize.Width, chart1.ClientSize.Height);
chart1.AntiAliasing = AntiAliasingStyles.Graphics;
chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
bmp.MakeTransparent(chart1.BackColor);
bmp.Save(yourFileName, ImageFormat.Png);

First we set some areas in the chart to a unique color; if you use fuchsia in your chart (yuck!), pick some other color.

Then we create a Bitmap to hold the image.

Now we turn anti-aliasing off for the text; this help to avoid funny colors when we turn the background pixels to tranparent.

Now we tell the chart control to draw itself into our bitmap.

Finally we make all pixels transparent that have our special color.

Now we can save to disk..

You may want to look into setting the dpi resolution and the chart size to make the result look nice in your pdf!

Update:

If you want to create the image without acutally showing a Chart control, there is no real problem, either. In fact you can even create a Chart image in a console application, if you include all necessary namespaces etc..

All you need to do is create the Chart in code and set up all necessary properties.

Here is a minimal example:

Chart chart = new Chart();
ChartArea CA = chart.ChartAreas.Add("A1");
Series S1 = chart.Series.Add("S1");
S1.ChartType = SeriesChartType.Pie;

S1.Points.AddXY(1, 17);
S1.Points.AddXY(2, 27);
S1.Points.AddXY(3, 7);
S1.Points.AddXY(4, 49);

chart.BackColor = Color.Fuchsia;
CA.BackColor = chart.BackColor;
CA.Area3DStyle.Enable3D = true;
S1.Points[2]["Exploded"] = "true";
Bitmap bmp = new Bitmap(chart.Width, chart.Height);
chart.AntiAliasing = AntiAliasingStyles.None;
chart.DrawToBitmap(bmp, chart.ClientRectangle);
bmp.MakeTransparent(chart.BackColor);
bmp.Save(yourFileName, ImageFormat.Png);

这篇关于使用C#的动态图形(要保存为PNG格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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