【发布时间】:2016-03-01 09:50:31
【问题描述】:
我必须动态生成动态图表,将其保存为 PNG 图像格式(透明背景)以嵌入到 PDF 中。
问题是,图表不会显示在页面中的控件上(WebForm 中没有图表控件),应该从基础信息生成,并且应该在创建 PDF 时嵌入。
用于 PDF 和 PDF 生成的库已准备就绪。
我想知道如何在没有适当的图表控件的情况下生成动态图形(条形图、饼图)或简单图形,并能够将其保存为具有透明背景 (.PNG) 的图像。
原因是PDF模板有很多图层阴影,因此图形图像周围的背景应该是透明的,类似于下图。
我已尝试生成与以下链接中的示例类似的动态条形图,但图像看起来不太理想
https://2leggedspider.wordpress.com/2004/11/21/generating-a-dynamic-bar-graph-using-aspnet-and-c/
非常感谢有关该功能的任何帮助。提前谢谢你。
更新:2016 年 3 月 16 日星期三
有了 TaW 的回答和更新,我已经能够绘制出所需的图表,下面是对我提出的同一问题的回答的更新
添加对System.Windows.Forms 和System.Windows.Forms.DataVisualization 的引用
在下面添加命名空间
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
下面的方法会给你如图所示边缘光滑的饼图
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。如果我错了,请纠正我。
【问题讨论】:
-
我觉得不错!很高兴看到抗锯齿像素确实是半透明的! (我刚刚在 Photoshop 中检查了您的图像!)