asp.net ChartControl控件导出图片

来源:互联网 发布:y2电机技术数据大全 编辑:程序博客网 时间:2024/06/06 10:04

asp.net前台页面中使用了DevExpress.XtraCharts.Web的WebChartControl控件来显示图表,代码如下,比较简单:

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td align="center">
        <dxchartsui:WebChartControl ID="chartControl1" runat="server">
        </dxchartsui:WebChartControl>
    </td>
</tr>
</table>

最终查询出数据后,图表显示效果如下:


在WinForm版的chartControl控件中有一个函数ExportToImage(Stream stream, ImageFormat format)能够直接导出生成图片。但是web版的没有,在官网的论坛上找到了相关的解决方法https://www.devexpress.com/Support/Center/Question/Details/Q22709/webchartcontrol-how-to-save-an-image-to-a-file

在这里整理一个完整的代码

protected void btnSaveAs_Click(object sender, EventArgs e)
{

    MemoryStream memoryImage = new MemoryStream();
    ((IChartContainer)this.chartControl1 as IChartContainer).Chart.ExportToImage(memoryImage, ImageFormat.Jpeg);
    memoryImage.Seek(0, System.IO.SeekOrigin.Begin);

    string fileName = "XXX曲线分布";    
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.jpeg", HttpUtility.UrlEncode(fileName), Encoding.UTF8));
    Response.BinaryWrite(memoryImage.ToArray());
}


原创粉丝点击