highcharts 导出图片 .net c#

 highcharts提供了导出图片功能,但是是需要连接他的服务器进行导出工作。

必须要上公网,并且国外服务器网速相当慢。

所以找了一个自己实现highcharts图片导出的类,放出来。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Svg;
using System.Drawing.Imaging;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
namespace ExportServer
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Form["type"] != null && Request.Form["svg"] != null && Request.Form["filename"] != null)
                {
                    string tType = Request.Form["type"].ToString();
                    string tSvg = Request.Form["svg"].ToString();
                    string tFileName = Request.Form["filename"].ToString();
                    if (tFileName == "")
                    {
                        tFileName = "chart";
                    }
                    MemoryStream tData = new MemoryStream(Encoding.UTF8.GetBytes(tSvg));
                    MemoryStream tStream = new MemoryStream();
                    string tTmp = new Random().Next().ToString();

                    string tExt = "";
                    string tTypeString = "";

                    switch (tType)
                    {
                        case "image/png":
                            tTypeString = "-m image/png";
                            tExt = "png";
                            break;
                        case "image/jpeg":
                            tTypeString = "-m image/jpeg";
                            tExt = "jpg";
                            break;
                        case "application/pdf":
                            tTypeString = "-m application/pdf";
                            tExt = "pdf";
                            break;
                        case "image/svg+xml":
                            tTypeString = "-m image/svg+xml";
                            tExt = "svg";
                            break;
                    }

                    if (tTypeString != "")
                    {
                        string tWidth = Request.Form["width"].ToString();
                        Svg.SvgDocument tSvgObj = SvgDocument.Open(tData);
                        switch (tExt)
                        {
                            case "jpg":
                                tSvgObj.Draw().Save(tStream, ImageFormat.Jpeg);
                                break;
                            case "png":
                                tSvgObj.Draw().Save(tStream, ImageFormat.Png);
                                break;
                            case "pdf":
                                PdfWriter tWriter = null;
                                Document tDocumentPdf = null;
                                try
                                {
                                    tSvgObj.Draw().Save(tStream, ImageFormat.Png);
                                    tDocumentPdf = new Document(new Rectangle((float)tSvgObj.Width, (float)tSvgObj.Height));
                                    tDocumentPdf.SetMargins(0.0f, 0.0f, 0.0f, 0.0f);
                                    iTextSharp.text.Image tGraph = iTextSharp.text.Image.GetInstance(tStream.ToArray());
                                    tGraph.ScaleToFit((float)tSvgObj.Width, (float)tSvgObj.Height);

                                    tStream = new MemoryStream();
                                    tWriter = PdfWriter.GetInstance(tDocumentPdf, tStream);
                                    tDocumentPdf.Open();
                                    tDocumentPdf.NewPage();
                                    tDocumentPdf.Add(tGraph);
                                    tDocumentPdf.CloseDocument();
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                                finally
                                {
                                    tDocumentPdf.Close();
                                    tDocumentPdf.Dispose();
                                    tWriter.Close();
                                    tWriter.Dispose();
                                    tData.Dispose();
                                    tData.Close();

                                }
                                break;

                            case "svg":
                                tStream = tData;
                                break;
                        }

                        Response.ClearContent();
                        Response.ClearHeaders();
                        Response.ContentType = tType;
                        Response.AppendHeader("Content-Disposition", "attachment; filename=" + tFileName + "." + tExt + "");
                        Response.BinaryWrite(tStream.ToArray());
                        Response.End();
                    }
                }
            }
        }
    }
}

需要引用svg.dll 一个开源项目连接自己找

itextsharp.dll 提供中文支持

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值