<%@ Page Language="C#" AutoEventWireup="true" CodeFile="code.aspx.cs" Inherits="code" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>鏃犳爣棰橀〉</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


    
/// <summary>
    
/// code 验证码生成页
    
/// </summary>

    public partial class code : System.Web.UI.Page
    
{
        
// 验证码长度
        private int codeLen = 8;
        
// 图片清晰度
        private int fineness = 80;
        
// 图片宽度
        private int imgWidth = 128;
        
// 图片高度
        private int imgHeight = 24;
        
// 字体家族名称
        private string fontFamily = "Times New Roman";
        
// 字体大小
        private int fontSize = 14;
        
// 字体样式
        private int fontStyle = 0;
        
// 绘制起始坐标 X
        private int posX = 0;
        
// 绘制起始坐标 Y
        private int posY = 0;

        
//------------------------------------------------------------
        
// code.aspx 页面加载函数
        
//------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        
{
            
读取 Request 传递参数

            
string code = Createcode();

            
// 生成BITMAP图像
            Bitmap bitmap = new Bitmap(imgWidth, imgHeight);

            
// 给图像设置干扰
            DisturbBitmap(bitmap);

            
// 绘制验证码图像
            Drawcode(bitmap, code);

            
// 保存验证码图像,等待输出
            bitmap.Save(Response.OutputStream, ImageFormat.Gif);
        }



        
//------------------------------------------------------------
        
// 随机生成验证码,并保存到SESSION中
        
//------------------------------------------------------------
        private string Createcode()
        
{
            
string code = "";

            
// 随机数对象
            Random random = new Random();

            
for (int i = 0; i < codeLen; i++)
            
{
                
// 26: a - z
                int n = random.Next(26);

                
// 将数字转换成大写字母
                code += (char)(n + 65);
            }


            
// 保存验证码
            Session["code"= code;

            
return code;
        }


        
//------------------------------------------------------------
        
// 为图片设置干扰点
        
//------------------------------------------------------------
        private void DisturbBitmap(Bitmap bitmap)
        
{
            
// 通过随机数生成
            Random random = new Random();

            
for (int i = 0; i < bitmap.Width; i++)
            
{
                
for (int j = 0; j < bitmap.Height; j++)
                
{
                    
if (random.Next(100<= this.fineness)
                        bitmap.SetPixel(i, j, Color.White);
                }

            }

            
        }


        
//------------------------------------------------------------
        
// 绘制验证码图片
        
//------------------------------------------------------------
        private void Drawcode(Bitmap bitmap, string code)
        
{
            
// 获取绘制器对象
            Graphics g = Graphics.FromImage(bitmap);

            
// 设置绘制字体
            Font font = new Font(fontFamily, fontSize, GetFontStyle());
            Pen myPen 
= new Pen(Color.Black,2);

            
//随机画两条线
            Random r = new Random();
            
float x1 = r.Next(imgWidth);
            
float x2 = r.Next(imgWidth) ;
            
float y1 = r.Next(imgHeight);
            
float y2 = r.Next(imgHeight);
            g.DrawLine(myPen, x1, y1, x2, y2);
             x1 
= r.Next(imgWidth );
             x2 
= r.Next(imgWidth);
             y1 
= r.Next(imgHeight);
             y2 
= r.Next(imgHeight);
            g.DrawLine(myPen, x1, y1, x2, y2);
            
// 绘制验证码图像
            g.DrawString(code, font, Brushes.Black, posX, posY);
            
        }


        
//------------------------------------------------------------
        
// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
        
//------------------------------------------------------------
        private FontStyle GetFontStyle()
        
{
            
if (fontStyle == 1)
                
return FontStyle.Bold;
            
else if (fontStyle == 2)
                
return FontStyle.Italic;
            
else if (fontStyle == 3)
                
return FontStyle.Bold | FontStyle.Italic;
            
else
                
return FontStyle.Regular;
        }

    }



posted on 2007-02-25 13:20  mbskys  阅读(242)  评论(0)    收藏  举报