ValidateImage.aspx.cs
--------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
for (int i = 0; i <= strimg.Length - 1; i++)
{
if (i == 0)
{
g.DrawString(strimg.Substring(0, 1), new Font("Dotum", 12), new SolidBrush(Color.DeepSkyBlue), new PointF(1.0F, 2.0F));
}
else if (i == 1)
{
g.DrawString(strimg.Substring(1, 1), new Font("Dotum", 12), new SolidBrush(Color.Coral), new PointF(11.0F, 2.0F));
}
else if (i == 2)
{
g.DrawString(strimg.Substring(2, 1), new Font("Dotum", 12), new SolidBrush(Color.DodgerBlue), new PointF(21.0F, 2.0F));
}
else if (i == 3)
{
g.DrawString(strimg.Substring(3, 1), new Font("Dotum", 12), new SolidBrush(Color.MediumBlue), new PointF(31.0F, 2.0F));
}
else if (i == 4)
{
g.DrawString(strimg.Substring(4, 1), new Font("Dotum", 12), new SolidBrush(Color.Magenta), new PointF(41.0F, 2.0F));
}
else if (i == 5)
{
g.DrawString(strimg.Substring(5, 1), new Font("Dotum", 12), new SolidBrush(Color.DeepSkyBlue), new PointF(51.0F, 2.0F));
}
}
imgOutput.Save(Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
imgOutput.Dispose();
Response.End();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
使用我新做的验证码控件就行了,免去好多麻烦。
WakiCheckDigit for asp.net 是适合于微软.Net开发平台的Web控件。它能放进Web 工具箱中,并可拖入 WEB 页面进行可视化的设置。它可以方便快速生成验证码,用于用户登录,身份验证等。可防止程序穷举破解用户信息。不用编程,一分钟生成安全验证系统,为你的系统构筑多一道安全防线。
下载地址:
http://www.aspxcontrol.com/download/wakicheckdigit.rar
<%@ Page Language="VB" debug=true%>
<%@ Import Namespace="system"%>
<%@ Import Namespace="system.Drawing"%>
<%@ Import Namespace="system.Drawing.Imaging"%>
<%@ Import Namespace="system.IO"%>
<script runat="server">
sub page_load(byval sender As Object,byval E As EventArgs)
dim My_Stream as MemoryStream=Get_Images(14,6,"#ffffff")
Show_image(My_Stream)'显示内存图像
My_Stream.Close()'关闭打开的流文件
end sub
'Font_Size'字体大小 intrger
'Char_Number'验证码的位数 intrger
'BackgroundColor'背景颜色 十六进制数的字符串
function Get_Images(Font_Size as integer,Char_Number as integer,BackgroundColor as string) as MemoryStream'把字符转换为图像,并且保存到内存流
dim image_w as integer=int(Font_Size*1.5)+Font_Size*Char_Number
dim image_h as integer=int(2.5*Font_Size)
dim Temp_Bitmap as Bitmap'封装GDI+位图
dim Temp_Graphics as Graphics'封装GDI+绘图面
dim Color_Back as Color=ColorTranslator.FromHtml(BackgroundColor)'背景颜色
Temp_Bitmap = new Bitmap(image_w, 5*image_h, PixelFormat.Format32bppRgb)'注意注 确定背景大小
Temp_Graphics = Graphics.FromImage(Temp_Bitmap)
Temp_Graphics.FillRectangle(new SolidBrush(Color_Back),new Rectangle(0, 0,image_w, 5*image_h))'注意注 绘制背景
DIM Sesson_Company AS String=""'为了进行验证比较
dim n as integer
for n= 0 to Char_Number-1
dim Show_Str as string=getChar()'要显示为图像的字符
Sesson_Company=Sesson_Company & Show_Str
dim Show_Str_Font_Size as integer=Int(3 * Rnd()+(Font_Size-2))'字体随机大小 下波动2
dim Color_Font as Color= ColorTranslator.FromHtml(getColor())'字体随机颜色
dim Show_Font_Name as string=getFont()'字体
dim Show_Str_Font as Font=new Font(Show_Font_Name,Show_Str_Font_Size,FontStyle.Bold)'定义文本格式(字体,字号,粗体)
Temp_Graphics.DrawString(Show_Str, Show_Str_Font, new SolidBrush(Color_Font), int(Font_Size/2)+n*Font_Size, Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4
next
session("imagenumber")=LCase(trim(Sesson_Company))'保存到session便于调用的页面比较 '不区分大小写
Font_Size=Font_Size*4
image_h=int(2.5*Font_Size)
dim Temp_Stream as MemoryStream=new MemoryStream()
Temp_Bitmap.Save(Temp_Stream, ImageFormat.jpeg)
Temp_Graphics.Dispose()'释放资源
Temp_Bitmap.Dispose()'释放资源
Temp_Stream.Close()'关闭打开的流文件
return Temp_Stream'返回流
end function
function Show_image(Show_Stream as MemoryStream)'显示内存图像
Response.ClearContent()
Response.ContentType = "Image/Jpeg"
Response.BinaryWrite(Show_Stream.ToArray())
Response.End()
end function
function getChar() as string'获得随机字符 0-9 a-z A-Z
Dim Char_array(5)
Char_array(0) = chr(Int(10 * Rnd()+48))
Char_array(1) = chr(Int(26 * Rnd()+65))
Char_array(2) = chr(Int(26 * Rnd()+65))
Char_array(3) = chr(Int(26 * Rnd()+97))
Char_array(4) = chr(Int(26 * Rnd()+97))
return Char_array(Int(5 * Rnd()))
end function
function getColor() as string'获得随机颜色
Dim int_a As integer
Dim int_b As integer
Dim int_c As integer
int_a=Int(180 * Rnd()+20)
int_b=Int(180 * Rnd()+20)
int_c=Int(180 * Rnd()+20)
if int_a>150 and int_b>150 and int_c>150 then int_a=Int(150 * Rnd()+20)
return "#" & Hex(int_a) & Hex(int_b) & Hex(int_c)
end function
function getFont() as string'获得随机字体
Dim font_array(7)
font_array(0) = "Book Antiqua"
font_array(1) = "Microsoft Sans Serif"
font_array(2) = "Comic Sans MS"
font_array(3) = "Comic Sans MS"
font_array(4) = "Comic Sans MS"
font_array(5) = "Comic Sans MS"
font_array(6) = "Comic Sans MS"
return font_array(Int(7 * Rnd()))
end function
using System;
using System.IO;
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;
public void ValidataCode(string str)
{
System.Drawing.Bitmap Img;
Graphics g;
MemoryStream ms;
int gheight = str.Length*11;
Img = new Bitmap(gheight,20);
g = Graphics.FromImage(Img);
SolidBrush sb = new SolidBrush(Color.DarkSlateBlue);
Font ft = new Font("Arial",10);
g.DrawString(str,ft,sb,3,3);
ms = new MemoryStream();
Img.Save(ms,ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
public string RndNum(int i)
{
string VNum = "";
Random Rnd = new Random();
string Vchar = "0,1,2,3,4,5,6,7,8,9,a,s,d,f,g,h,j,k,l,q,w,e,r,t,y,u,i,o,p,z,x,c,v,b,n,m,Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M";
string[] Vc = Vchar.Split(',');
for(int k =0;k<i;k++)
{
VNum = VNum + Vc[Rnd.Next(0,61)];
}
return VNum;
}
}
}