一般处理程序生成图像

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Drawing.Drawing2D;
using System.Drawing;



namespace AspTechnicalBase
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class DrawingOnWebPage : IHttpHandler
    {    /// <summary>
        /// 绘图的相关参数
        /// </summary>
        private const int ImageWidth = 600;
        private const int ImageHeight = 100;

        /// <summary>
        /// 响应HTTP请求
        /// </summary>
        /// <param name="context">HTTP请求的上下文对象</param>
        public void ProcessRequest(HttpContext context)
        {

            //开辟一个ImageWidth像素宽、ImageHeight像素高的绘图缓冲区
            Bitmap bmp = new Bitmap(ImageWidth, ImageHeight);
            //创建绘图表面对象,引用这一绘图缓冲区
            Graphics g = Graphics.FromImage(bmp);

            //生成一个渐变的画刷
            Brush br = new LinearGradientBrush(new Point(0, 0), new Point(ImageWidth, 0), Color.Black, Color.Blue);
            //用渐变画刷填充绘图表面
            g.FillRectangle(br, g.ClipBounds);

            string str = "使用GDI+动态生成Web图像";
            Font f = new Font("宋体", 30);
            //输出文字
            g.DrawString(str, f, Brushes.Yellow, new PointF(40, 20));

            //将结果输出到浏览器
            context.Response.ContentType = "Image/JPEG";
            context.Response.Clear();
            context.Response.BufferOutput = true;
            bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            //释放相关的绘图对象
            br.Dispose();
            f.Dispose();
            g.Dispose();
            bmp.Dispose();


        }
        /// <summary>
        /// 是否自动缓存此对象以供下次复用
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}

 

使用:

 

    <form id="form1" runat="server">
    <div>
    <h2>使用一般处理程序绘制Web图像</h2>
    <hr />
    <img src="DrawingOnWebPage.ashx" alt="动态生成的GDI+图像" />
    </div>
    </form>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值