highcharts 匯出圖片 .net c#

來源:互聯網
上載者:User

 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 提供中文支援

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.