加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

一般处理程序生成图像

发布时间:2020-12-17 02:08:48 所属栏目:安全 来源:网络整理
导读: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; na

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,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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读