一般处理程序 —— GDI
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _2_9_输出一幅动态创建的图片 { class Program { //GDI:.Net程序中进行绘图的一些类 static void Main(string[] args) { using (Bitmap bmp = new Bitmap(500, 500))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 { g.DrawString("动态创建的图片", new Font(FontFamily.GenericSerif, 30), Brushes.Red, 100, 100);//Font应该被释放 g.DrawEllipse(Pens.Blue, 100, 100, 100, 100); //bmp.Save(@"一幅动态创建的图片.jpg", ImageFormat.Jpeg);//图片保存到输出流 using (Stream stream = File.OpenWrite(@"一幅动态创建的图片.jpg")) { bmp.Save(stream, ImageFormat.Jpeg); } } } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Web; namespace _2_一般处理程序 { /// <summary> /// 泡妞证 的摘要说明 /// </summary> public class 泡妞证 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; string name = context.Request["Name"]; string imgFullPath = context.Server.MapPath("~/PaoNiuZheng.jpg"); using (Image bmp = Bitmap.FromFile(imgFullPath))//读取一张已有的图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 using (Font font1 = new Font(FontFamily.GenericSerif, 12)) using (Font font2 = new Font(FontFamily.GenericSerif, 5)) { { g.DrawString(name, font1, Brushes.Black, 125, 220);//Font应该被释放 g.DrawString(name, font2, Brushes.Black, 310, 50);//Font应该被释放 bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 } } } public bool IsReusable { get { return false; } } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Web; namespace _2_一般处理程序 { /// <summary> /// 输出一幅动态创建的图片 的摘要说明 /// </summary> public class 输出一幅动态创建的图片 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; string name = context.Request["name"]; //context.Response.Write("Hello World"); using (Bitmap bmp = new Bitmap(500, 500))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 { g.DrawString(name, new Font(FontFamily.GenericSerif, 30), Brushes.Red, 100, 100);//Font应该被释放 g.DrawEllipse(Pens.Blue, 100, 100, 100, 100); //bmp.Save(@"一幅动态创建的图片.jpg", ImageFormat.Jpeg);//图片保存到输出流 //直接保存到网页输出流中 bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 } } public bool IsReusable { get { return false; } } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Web; namespace _2_一般处理程序 { /// <summary> /// 显示访问者信息 的摘要说明 /// </summary> public class 显示访问者信息 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; using (Bitmap bmp = new Bitmap(500, 200))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 using (Font font = new Font(FontFamily.GenericSerif, 30)) { HttpRequest request = context.Request; g.DrawString("IP:" + request.UserHostAddress, font, Brushes.Red, 0, 0); g.DrawString("浏览器:" + request.Browser.Browser + request.Browser.Version, font, Brushes.Red, 0, 50); g.DrawString("操作系统:" + request.Browser.Platform, font, Brushes.Red, 0, 100); bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 } } public bool IsReusable { get { return false; } } } }
验证码:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>登录</title> <script type="text/javascript"> function RefreshCode() { var CheckCode = document.getElementById("CheckCode"); CheckCode.src = "CheckCode.ashx?t="+new Date(); } </script> </head> <body> <form action="Login.ashx" method="post"> 姓名:<input type="text" name="txtName" /> 密码:<input type="password" name="txtPwd" /> 验证码:<img src="CheckCode.ashx" id="CheckCode" onclick="RefreshCode()" /> <input type="text" name="txtCode" /> <input type="submit" name="btnLogin" value="提交" /> <div style="padding:10px; color:red;">{^msg$}</div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Web; namespace _2_一般处理程序 { /// <summary> /// 验证码 的摘要说明 /// </summary> public class 验证码 : IHttpHandler { public void ProcessRequest(HttpContext context) { //动态生成一张200*50的图片,显示一个随机的4位数。 context.Response.ContentType = "image/jpeg"; using (Bitmap bmp = new Bitmap(200, 50))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 using (Font font = new Font(FontFamily.GenericSerif, 30)) { Random rd = new Random(); string num = " "; for (int i = 0; i < 4; i++) { num += rd.Next(0,9)+" "; } HttpRequest request = context.Request; g.DrawString(num.ToString(), font, Brushes.Red,30, 0); bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 } } public bool IsReusable { get { return false; } } } }