验证码类源代码

以下是一个验证码类源代码,使用方法:建立一个ValidateImg.aspx,后台代码里写:ValidateImage img = new ValidateImage();验证的地方:ValidateImage.Validate(txtCode.Text.Trim())
  1   /**/ /// <summary>
  2    /// 验证码类
  3    /// </summary>

  4      public   class  ValidateImage
  5      {
  6        /**//// <summary>
  7        /// 要显示的文字
  8        /// </summary>

  9        public string Text
 10        {
 11            get return this.text; }
 12        }

 13        /**//// <summary>
 14        /// 图片
 15        /// </summary>

 16        public Bitmap Image
 17        {
 18            get return this.image; }
 19        }

 20        /**//// <summary>
 21        /// 宽度
 22        /// </summary>

 23        public int Width
 24        {
 25            get return this.width; }
 26        }

 27        /**//// <summary>
 28        /// 高度
 29        /// </summary>

 30        public int Height
 31        {
 32            get return this.height; }
 33        }

 34
 35        private string text;
 36        private int width;
 37        private int height;
 38        private Bitmap image;
 39
 40        private static byte[] randb = new byte[4];
 41        private static Random rand = new Random();
 42
 43        public ValidateImage()
 44        {
 45            text = rand.Next(10009999).ToString();
 46            System.Web.HttpContext.Current.Session["CheckCode"= text;
 47            this.width = (int)Math.Ceiling(text.Length * 16.5);
 48            this.height = 30;
 49
 50            GenerateImage();
 51            System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";
 52            Image.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
 53
 54        }

 55
 56        public static bool Validate(string input)
 57        {
 58            return System.Web.HttpContext.Current.Session["CheckCode"!= null && System.Web.HttpContext.Current.Session["CheckCode"].ToString().Equals(input);
 59        }

 60
 61        ~ValidateImage()
 62        {
 63            Dispose(false);
 64        }

 65
 66        public void Dispose()
 67        {
 68            GC.SuppressFinalize(this);
 69            this.Dispose(true);
 70        }

 71
 72        protected virtual void Dispose(bool disposing)
 73        {
 74            if (disposing)
 75                this.image.Dispose();
 76        }

 77        private FontFamily[] fonts = {
 78                                         new FontFamily("Times New Roman"),
 79                                         new FontFamily("Georgia"),
 80                                         new FontFamily("Arial"),
 81                                         new FontFamily("Comic Sans MS")
 82                                     }
;
 83
 84        public int Next(int max)
 85        {
 86            return rand.Next(max);
 87        }

 88
 89        /**//// <summary>
 90        /// 生成验证码图片
 91
 92        /// </summary>

 93        private void GenerateImage()
 94        {
 95            Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
 96
 97            Graphics g = Graphics.FromImage(bitmap);
 98            Rectangle rect = new Rectangle(00this.width, this.height);
 99            g.SmoothingMode = SmoothingMode.AntiAlias;
100
101            g.Clear(Color.White);
102
103            //int emSize = Next(3) + 15;//(int)((this.width - 20) * 2 / text.Length);
104            //int emSize = (int)((this.width - 20) * 2 / text.Length);
105
106            int emSize = 12;
107            FontFamily family = fonts[Next(fonts.Length - 1)];
108            Font font = new Font(family, emSize, FontStyle.Bold);
109
110            SizeF measured = new SizeF(00);
111            SizeF workingSize = new SizeF(this.width, this.height);
112            while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
113            {
114                font.Dispose();
115                font = new Font(family, emSize -= 2);
116            }

117
118            SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(80), Next(80)));
119            for (int x = 0; x < 1; x++)
120            {
121                Pen linePen = new Pen(Color.FromArgb(Next(150), Next(100), Next(100)), 1);
122                g.DrawLine(linePen, new PointF(0.0F + Next(2), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height - 10)));
123            }

124
125            for (int x = 0; x < this.text.Length; x++)
126            {
127                drawBrush.Color = Color.FromArgb(Next(150+ 20, Next(150+ 20, Next(150+ 20);
128                PointF drawPoint = new PointF(0.0F + Next(5+ x * 152.0F + Next(4));
129                g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
130            }

131
132            double distort = rand.Next(510* (Next(10== 1 ? 1 : -1);
133
134            using (Bitmap copy = (Bitmap)bitmap.Clone())
135            {
136                for (int y = 0; y < height; y++)
137                {
138                    for (int x = 0; x < width; x++)
139                    {
140                        int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
141                        int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
142                        if (newX < 0 || newX >= width) newX = 0;
143                        if (newY < 0 || newY >= height) newY = 0;
144                        bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
145                    }

146                }

147            }

148
149
150            //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
151
152            font.Dispose();
153            drawBrush.Dispose();
154            g.Dispose();
155
156            this.image = bitmap;
157        }

158    }
posted on 2008-06-09 15:41 YongGe 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/YongGe/archive/2008/06/09/1216245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值