通过IHttpHandler实现图片验证码
生活随笔
收集整理的這篇文章主要介紹了
通过IHttpHandler实现图片验证码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using?System;
using?System.Drawing;
using?System.Drawing.Imaging;
using?System.Text;
using?System.Web;
using?System.Web.SessionState;
namespace?HttpModuleDemo
{
????///?<summary>
????///?VerificationCode?的摘要說明
????///?</summary>
????public?class?VerificationCode?:?IHttpHandler,IRequiresSessionState?
????{
????????public?void?ProcessRequest(HttpContext?context)
????????{
????????????context.Response.ContentType?=?"image/jpeg";
????????????//建立Bitmap對象,繪圖
????????????//Bitmap:是用于處理由像素數據定義的圖像的對象
????????????Bitmap?bitmap?=?new?Bitmap(200,60);
????????????//Graphics:封裝一個?GDI+?繪圖圖面
????????????Graphics?graph?=?Graphics.FromImage(bitmap);
????????????graph.FillRectangle(new?SolidBrush(Color.White),?0,?0,?200,?60);
????????????Font?font?=?new?Font(FontFamily.GenericSerif,?48,?FontStyle.Bold,?GraphicsUnit.Pixel);
????????????Random?r?=?new?Random();
????????????string?letters?=?"ABCDEFGHIJKLMNPQRSTUVWXYZ";
????????????string?letter;
????????????StringBuilder?s?=?new?StringBuilder();
????????????
????????????//添加隨機的五個字母
????????????for?(int?x?=?0;?x?<?5;?x++)
????????????{
????????????????letter?=?letters.Substring(r.Next(0,?letters.Length?-?1),?1);
????????????????s.Append(letter);
????????????????graph.DrawString(letter,?font,?new?SolidBrush(Color.Black),?x?*?38,?r.Next(0,?15));
????????????}
????????????/*
????????????//混淆背景
????????????Pen?linePen?=?new?Pen(new?SolidBrush(Color.Black),?2);
????????????for?(int?x?=?0;?x?<?6;?x++)
????????????????graph.DrawLine(linePen,?new?Point(r.Next(0,?199),?r.Next(0,?59)),?new?Point(r.Next(0,?199),?r.Next(0,?59)));
????????????*/
????????????//將圖片保存到輸出流中??????
????????????bitmap.Save(context.Response.OutputStream,?ImageFormat.Jpeg);
????????????////如果沒有實現IRequiresSessionState,則這里會出錯,也無法生成圖片
????????????context.Session["CheckCode"]?=?s.ToString();
????????????context.Response.End();???
????????}
????????public?bool?IsReusable
????????{
????????????get
????????????{
????????????????return?false;
????????????}
????????}
????}
}
using?System.Drawing;
using?System.Drawing.Imaging;
using?System.Text;
using?System.Web;
using?System.Web.SessionState;
namespace?HttpModuleDemo
{
????///?<summary>
????///?VerificationCode?的摘要說明
????///?</summary>
????public?class?VerificationCode?:?IHttpHandler,IRequiresSessionState?
????{
????????public?void?ProcessRequest(HttpContext?context)
????????{
????????????context.Response.ContentType?=?"image/jpeg";
????????????//建立Bitmap對象,繪圖
????????????//Bitmap:是用于處理由像素數據定義的圖像的對象
????????????Bitmap?bitmap?=?new?Bitmap(200,60);
????????????//Graphics:封裝一個?GDI+?繪圖圖面
????????????Graphics?graph?=?Graphics.FromImage(bitmap);
????????????graph.FillRectangle(new?SolidBrush(Color.White),?0,?0,?200,?60);
????????????Font?font?=?new?Font(FontFamily.GenericSerif,?48,?FontStyle.Bold,?GraphicsUnit.Pixel);
????????????Random?r?=?new?Random();
????????????string?letters?=?"ABCDEFGHIJKLMNPQRSTUVWXYZ";
????????????string?letter;
????????????StringBuilder?s?=?new?StringBuilder();
????????????
????????????//添加隨機的五個字母
????????????for?(int?x?=?0;?x?<?5;?x++)
????????????{
????????????????letter?=?letters.Substring(r.Next(0,?letters.Length?-?1),?1);
????????????????s.Append(letter);
????????????????graph.DrawString(letter,?font,?new?SolidBrush(Color.Black),?x?*?38,?r.Next(0,?15));
????????????}
????????????/*
????????????//混淆背景
????????????Pen?linePen?=?new?Pen(new?SolidBrush(Color.Black),?2);
????????????for?(int?x?=?0;?x?<?6;?x++)
????????????????graph.DrawLine(linePen,?new?Point(r.Next(0,?199),?r.Next(0,?59)),?new?Point(r.Next(0,?199),?r.Next(0,?59)));
????????????*/
????????????//將圖片保存到輸出流中??????
????????????bitmap.Save(context.Response.OutputStream,?ImageFormat.Jpeg);
????????????////如果沒有實現IRequiresSessionState,則這里會出錯,也無法生成圖片
????????????context.Session["CheckCode"]?=?s.ToString();
????????????context.Response.End();???
????????}
????????public?bool?IsReusable
????????{
????????????get
????????????{
????????????????return?false;
????????????}
????????}
????}
}
?
轉載于:https://www.cnblogs.com/AngelLee2009/archive/2011/10/29/2228278.html
總結
以上是生活随笔為你收集整理的通过IHttpHandler实现图片验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做靠谱的程序员--《程序员修炼之道》读书
- 下一篇: ObjectArx开发对txt文本文件的