123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Drawing;
- using System.Web.UI.WebControls;
- using System.Drawing.Imaging;
- using System.Web.SessionState;
- namespace Bowin.Common
- {
- public class VerificationCodeModule : IHttpModule, IRequiresSessionState
- {
- public void Dispose()
- { }
- public void Init(HttpApplication context)
- {
- context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
- }
- void context_PreRequestHandlerExecute(object sender, EventArgs e)
- {
- HttpApplication context = (HttpApplication)sender;
- string str = context.Request.Url.ToString();
- if (str.Contains("verificationCode.png"))
- {
- //context.Response.Cache.SetNoStore();
- this._random = new Random();
- this._code = GetRandomCode();
- context.Session["code"] = this._code;
- this.SetPageNoCache(context);
- this.OnPaint(context);
- context.Response.End();
- }
- else
- { return; }
- }
- static string[] FontItems = new string[]
- {
- "Arial",
- "Helvetica",
- "Geneva",
- "sans-serif",
- "Verdana"
- };
- static Brush[] BrushItems = new Brush[]
- {
- Brushes.OliveDrab,
- Brushes.ForestGreen,
- Brushes.DarkCyan,
- Brushes.LightSlateGray,
- Brushes.RoyalBlue,
- Brushes.SlateBlue,
- Brushes.DarkViolet,
- Brushes.MediumVioletRed,
- Brushes.IndianRed,
- Brushes.Firebrick,
- Brushes.Chocolate,
- Brushes.Peru,
- Brushes.Goldenrod
- };
- static string[] BrushName = new string[] {
- "OliveDrab",
- "ForestGreen",
- "DarkCyan",
- "LightSlateGray",
- "RoyalBlue",
- "SlateBlue",
- "DarkViolet",
- "MediumVioletRed",
- "IndianRed",
- "Firebrick",
- "Chocolate",
- "Peru",
- "Goldenrod"
- };
- private static Color BackColor = Color.White;
- private static Pen BorderColor = Pens.DarkGray;
- private static int Width = 52;
- private static int Height = 21;
- private Random _random;
- private string _code;
- private int _brushNameIndex;
- /**/
- /**/
- /**/
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- //this.Load += new System.EventHandler(this.Page_Load);
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 设置页面不被缓存
- /// </summary>
- private void SetPageNoCache(HttpApplication context)
- {
- context.Response.Buffer = true;
- context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
- context.Response.Expires = 0;
- context.Response.CacheControl = "no-cache";
- context.Response.AppendHeader("Pragma", "No-Cache");
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 取得一个 4 位的随机码
- /// </summary>
- /// <returns></returns>
- private string GetRandomCode()
- {
- return Guid.NewGuid().ToString().Substring(0, 4);
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 随机取一个字体
- /// </summary>
- /// <returns></returns>
- private Font GetFont()
- {
- int fontIndex = _random.Next(0, FontItems.Length);
- FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
- return new Font(FontItems[fontIndex], 12, fontStyle);
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 取一个字体的样式
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- private FontStyle GetFontStyle(int index)
- {
- switch (index)
- {
- case 0:
- return FontStyle.Bold;
- case 1:
- return FontStyle.Italic;
- default:
- return FontStyle.Regular;
- }
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 随机取一个笔刷
- /// </summary>
- /// <returns></returns>
- private Brush GetBrush()
- {
- int brushIndex = _random.Next(0, BrushItems.Length);
- _brushNameIndex = brushIndex;
- return BrushItems[brushIndex];
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 绘画事件
- /// </summary>
- private void OnPaint(HttpApplication context)
- {
- Bitmap objBitmap = null;
- Graphics g = null;
- try
- {
- objBitmap = new Bitmap(Width, Height);
- g = Graphics.FromImage(objBitmap);
- Paint_Background(g);
- Paint_Text(g);
- Paint_TextStain(objBitmap);
- Paint_Border(g);
- objBitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
- context.Response.ContentType = "image/gif";
- }
- catch { }
- finally
- {
- if (null != objBitmap)
- objBitmap.Dispose();
- if (null != g)
- g.Dispose();
- }
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 绘画背景颜色
- /// </summary>
- /// <param name="g"></param>
- private void Paint_Background(Graphics g)
- {
- g.Clear(BackColor);
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 绘画边框
- /// </summary>
- /// <param name="g"></param>
- private void Paint_Border(Graphics g)
- {
- g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 绘画文字
- /// </summary>
- /// <param name="g"></param>
- private void Paint_Text(Graphics g)
- {
- g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// 绘画文字噪音点
- /// </summary>
- /// <param name="g"></param>
- private void Paint_TextStain(Bitmap b)
- {
- for (int n = 0; n < 30; n++)
- {
- int x = _random.Next(Width);
- int y = _random.Next(Height);
- b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
- }
- }
- }
- }
|