日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过IHttpHandler实现图片验证码

發布時間:2024/4/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过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;
????????????}
????????}
????}
}

?

轉載于:https://www.cnblogs.com/AngelLee2009/archive/2011/10/29/2228278.html

總結

以上是生活随笔為你收集整理的通过IHttpHandler实现图片验证码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。