WEB控件中怎么用GDI画图

来源:百度知道 编辑:UC知道 时间:2024/05/30 03:43:06
windows控件一样继承System.Windows.Forms.UserControl,可以直接在paint里面画,但编译出的dll不能显示在网页上。
所以我要改成WEB控件,新建项目后,看到它是继承System.Web.UI.WebControls.WebControl,
用Render输出到网页,比如我想画一个图表,自己用GDI+写内容。在哪儿写?

可以参考下面的代码“Handler.ashx”
先在Bitmap上绘图,再把画好的图以jpeg的格式发给客户端。

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Drawing;
using System.IO;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";
Bitmap bmp = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("abc", SystemFonts.CaptionFont, Brushes.White, 0, 0);
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

public bool IsReusable {
get {
return false;
}
}

}

GDI+画一个图表,保存到输出流,