c#实现amcharts图片导出

本文详细介绍了如何使用C#在AmCharts图表中启用图片导出功能。首先,设置控件属性ExportAsImageEnabled为true并配置ExportAsImageMessageText。接着,编辑XX_settings.xml文件,正确设定<file>和<target>路径。然后创建一个接收导出数据的aspx文件,并在aspx.cs后台代码中解析请求,生成Bitmap图像并设置像素颜色,最后以JPEG格式响应输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现amcharts导出图片功能详解:

1、控件属性设置

至于控件如何加载到项目中我就不在多说了!baidu吧!

右键点击选控件属性,找到ExportAsImageEnabled设置成true,找到ExportAsImageMessageText设置成导出图片

2、XX_settings.xml设置

在amcharts目录下找到对应的xml文件,填写<file></file>和<target></target>完成设置

注意路径要写对

<export_as_image>

   <file>ampie/export.aspx</file>

   <target>_blank</target>

   <x></x>
   <y></y>

   <color></color>
   <alpha></alpha>

   <text_color></text_color>

   <text_size></text_size>

</export_as_image>

3、添加接收数据的文件

aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="export.aspx.cs" Inherits="_export" %>

aspx.cs文件

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;


namespace amCharts
{
    public partial class _export : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["width"] != null && Request.Form["width"] != String.Empty)
            {
                // image dimensions
                int width = Int32.Parse((Request.Form["width"].IndexOf('.') != -1) ? Request.Form["width"].Substring(0, Request.Form["width"].IndexOf('.')) : Request.Form["width"]);
                int height = Int32.Parse((Request.Form["height"].IndexOf('.') != -1) ? Request.Form["height"].Substring(0, Request.Form["height"].IndexOf('.')) : Request.Form["height"]);

                // image
                Bitmap result = new Bitmap(width, height);

                // set pixel colors
                for (int y = 0; y < height; y++)
                {
                    // column counter for the row
                    int x = 0;
                    // get current row data
                    string[] row = Request.Form["r" + y].Split(new char[] { ',' });
                    // set pixels in the row
                    for (int c = 0; c < row.Length; c++)
                    {
                        // get pixel color and repeat count
                        string[] pixel = row[c].Split(new char[] { ':' });
                        Color current_color = ColorTranslator.FromHtml("#" + pixel[0]);
                        int repeat = pixel.Length > 1 ? Int32.Parse(pixel[1]) : 1;

                        // set pixel(s)
                        for (int l = 0; l < repeat; l++)
                        {
                            result.SetPixel(x, y, current_color);
                            x++;
                        }
                    }
                }

                // output image

                // image type
                Response.ContentType = "image/jpeg";
                Response.AddHeader("Content-Disposition", "attachment; filename=/"amchart.jpg/"");

                // find image encoder for selected type
                ImageCodecInfo[] encoders;
                ImageCodecInfo img_encoder = null;
                encoders = ImageCodecInfo.GetImageEncoders();
                foreach (ImageCodecInfo codec in encoders)
                    if (codec.MimeType == Response.ContentType)
                    {
                        img_encoder = codec;
                        break;
                    }

                // image parameters
                EncoderParameter jpeg_quality = new EncoderParameter(Encoder.Quality, 100L); // for jpeg images only
                EncoderParameters enc_params = new EncoderParameters(1);
                enc_params.Param[0] = jpeg_quality;

                result.Save(Response.OutputStream, img_encoder, enc_params);
            }
            else
            {
                // invalid post
                Response.Write("Invalid post");
            }
        }
    }
}

至此项目就可以导出图片了!

在页面中点击右键就可以看到“导出图片”的选项,试一试 OK吗?

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值