验证码 完整代码

1 using  System;
  2
using  System.Collections;
  3
using  System.ComponentModel;
  4
using  System.Data;
  5
using  System.Drawing;
  6
using  System.Drawing.Imaging;
  7
using  System.Web;
  8
using  System.Web.SessionState;
  9
using  System.Web.UI;
 10
using  System.Web.UI.WebControls;
 11
using  System.Web.UI.HtmlControls;
 
12
 13
public   partial   class  yanzzengma : System.Web.UI.Page
 
14 {
 
15    // 验证码长度
 16    private int codeLen = 4;
 
17    // 图片清晰度
 18    private int fineness =90;
 
19    // 图片宽度
 20    private int imgWidth = 55;
 
21    // 图片高度
 22    private int imgHeight = 24;
 
23    // 字体家族名称
 24    private string fontFamily = "Times New Roman";
 
25    // 字体大小
 26    private int fontSize = 14;
 
27    // 字体样式
 28    private int fontStyle = 0;
 
29    // 绘制起始坐标 X
 30    private int posX = 0;
 
31    // 绘制起始坐标 Y
 32    private int posY = 0;
 
33    protected void Page_Load(object sender, EventArgs e)
 
34    {
 
35        读取 Request 传递参数读取 Request 传递参数
163
164        string validateCode = CreateValidateCode();
165
166        // 生成BITMAP图像
167        Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
168
169        // 给图像设置干扰
170        DisturbBitmap(bitmap);
171
172        // 绘制验证码图像
173        DrawValidateCode(bitmap, validateCode);
174
175        // 保存验证码图像,等待输出
176        bitmap.Save(Response.OutputStream, ImageFormat.Gif);
177    }

178
179    Web 窗体设计器生成的代码Web 窗体设计器生成的代码
198
199    //------------------------------------------------------------
200    // 随机生成验证码,并保存到SESSION中
201    //------------------------------------------------------------
202    private string CreateValidateCode()
203    {
204        string validateCode = "";
205        int num=0;
206        // 随机数对象
207        Random random = new Random();
208
209        for (int i = 0; i < codeLen; i++)
210        {
211            // 26: a - z
212            int n = random.Next(26);
213           
214
215            // 将数字转换成大写字母
216            validateCode += (char)(n + 65);
217        }

218     
219        // 保存验证码
220        Session["ValidateCode"= validateCode;
221
222        return validateCode;
223    }

224
225    //------------------------------------------------------------
226    // 为图片设置干扰点
227    //------------------------------------------------------------
228    private void DisturbBitmap(Bitmap bitmap)
229    {
230        // 通过随机数生成
231        Random random = new Random();
232
233        for (int i = 0; i < bitmap.Width; i++)
234        {
235            for (int j = 0; j < bitmap.Height; j++)
236            {
237                if (random.Next(100<= this.fineness)
238                    bitmap.SetPixel(i, j, Color.White);
239            }

240        }

241    }

242
243    //------------------------------------------------------------
244    // 绘制验证码图片
245    //------------------------------------------------------------
246    private void DrawValidateCode(Bitmap bitmap, string validateCode)
247    {
248        // 获取绘制器对象
249        Graphics g = Graphics.FromImage(bitmap);
250
251        // 设置绘制字体
252        Font font = new Font(fontFamily, fontSize, GetFontStyle());
253
254        // 绘制验证码图像
255        g.DrawString(validateCode, font, Brushes.Black, posX, posY);
256    }

257
258    //------------------------------------------------------------
259    // 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
260    //------------------------------------------------------------
261    private FontStyle GetFontStyle()
262    {
263        if (fontStyle == 1)
264            return FontStyle.Bold;
265        else if (fontStyle == 2)
266            return FontStyle.Italic;
267        else if (fontStyle == 3)
268            return FontStyle.Bold | FontStyle.Italic;
269        else
270            return FontStyle.Regular;
271    }

272
273
274}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值