📄 imagehandler.ashx
字号:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
//创建Bitmap
Bitmap imageTime = new Bitmap(500, 100);
//从Bitmap取得Image
Graphics g = Graphics.FromImage(imageTime);
//将Bitmap背景色填满LightPink颜色
SolidBrush colorPen = new SolidBrush(Color.White);
g.FillRectangle(colorPen, 0, 0, 500, 800);
//定义画笔及样式
//取得HatchStyle列举类型的所有成员
Array obj = Enum.GetValues(typeof(HatchStyle));
int valueStyle = new Random().Next(obj.Length);
HatchStyle brushStyle = (HatchStyle)obj.GetValue(valueStyle);
HatchBrush theBrush = new HatchBrush(brushStyle, Color.White, Color.Black);
//在Bitmap上绘制GDI+时间字体
g.DrawString(DateTime.Now.ToLongTimeString(), new Font("Arial Black", 48), theBrush, 0, 0);
//建立MemoryStream
MemoryStream ms = new MemoryStream();
//将BitMap存入MemoryStream
imageTime.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
//新建二进位的Byte数组
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
//将数据写入到buffer之中
ms.Read(buffer, 0, (int)ms.Length);
ms.Close();
//将buffer输出成数据流
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
//关闭数据流
context.Response.OutputStream.Close();
buffer = null;
imageTime.Dispose();
g.Dispose();
}
public bool IsReusable
{
get
{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -