您的位置:首页 > 编程语言 > C#

c#实现amcharts图片导出

2010-05-22 14:04 411 查看
实现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吗?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: