附加码生产程序

1@ Page Language="VB" debug=true
1@ Import Namespace="system"
1@ Import Namespace="system.Drawing"
1@ Import Namespace="system.Drawing.Imaging"
1@ Import Namespace="system.IO"
  1<script runat="server">   
  2'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   
  3'' 附加码生产程序 附加码生产程序 附加码生产程序 附加码生产程序 ''   
  4'' ''   
  5'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   
  6'' ''   
  7'' 为 教 师 节 献 礼 ''   
  8'' ''   
  9'' 今天要写附加码验证程序,本来想偷懒,可在网上搜索了好久没有一断满意的代码, ''   
 10''索性就自己写了这段代码,运行后觉得效果不错!不敢私藏,特拿来给大家分享!以此感 ''   
 11''谢CSDN、以及在CSDN的高手们!! ''   
 12'' ''   
 13'' ''   
 14'' 望大家发表你尊贵的意见! ''   
 15'' ''   
 16''  [email protected]  ''   
 17'' 2004年9月10日 ''   
 18'' ''   
 19'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   
 20sub page_load(byval sender As Object,byval E As EventArgs)   
 21'Font_Size'字体大小 intrger   
 22'Char_Number'验证码的位数 intrger   
 23'BackgroundColor'背景颜色 十六进制数的字符串   
 24dim My_Stream as MemoryStream=Get_Images(20,50,"#000000")   
 25Show_image(My_Stream)'显示内存图像   
 26My_Stream.Close()'关闭打开的流文件   
 27end sub 
 28
 29'Font_Size'字体大小 intrger   
 30'Char_Number'验证码的位数 intrger   
 31'BackgroundColor'背景颜色 十六进制数的字符串   
 32function Get_Images(Font_Size as integer,Char_Number as integer,BackgroundColor as string) as MemoryStream'把字符转换为图像,并且保存到内存流   
 33dim image_w as integer=int(Font_Size*1.5)+Font_Size*Char_Number'这个数字在调用页面需要,你要自己算出明确的数值 注意注意注意注意!!!!!   
 34dim image_h as integer=int(2.5*Font_Size)'这个数字在调用页面需要,你要自己算出明确的数值 注意注意注意注意!!!!!   
 35  
 36dim Temp_Bitmap as Bitmap'封装GDI+位图   
 37dim Temp_Graphics as Graphics'封装GDI+绘图面   
 38dim Color_Back as Color=ColorTranslator.FromHtml(BackgroundColor)'背景颜色   
 39  
 40Temp_Bitmap = new Bitmap(image_w, 5*image_h, PixelFormat.Format32bppRgb)'注意注 确定背景大小   
 41  
 42Temp_Graphics = Graphics.FromImage(Temp_Bitmap)   
 43Temp_Graphics.FillRectangle(new SolidBrush(Color_Back),new Rectangle(0, 0,image_w, 5*image_h))'注意注 绘制背景   
 44  
 45DIM Sesson_Company AS String=""'为了进行验证比较   
 46dim n as integer   
 47for n= 0 to Char_Number-1   
 48dim Show_Str as string=getChar()'要显示为图像的字符   
 49Sesson_Company=Sesson_Company & Show_Str   
 50dim Show_Str_Font_Size as integer=Int(3 * Rnd()+(Font_Size-2))'字体随机大小 下波动2   
 51dim Color_Font as Color= ColorTranslator.FromHtml(getColor())'字体随机颜色   
 52dim Show_Font_Name as string=getFont()'字体   
 53dim Show_Str_Font as Font=new Font(Show_Font_Name,Show_Str_Font_Size,FontStyle.Bold)'定义文本格式(字体,字号,粗体)   
 54Temp_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   
 55next   
 56session("imagenumber")=LCase(trim(Sesson_Company))'保存到session便于调用的页面比较 '不区分大小写   
 57  
 58Font_Size=Font_Size*4   
 59image_h=int(2.5*Font_Size)   
 60Temp_Graphics.DrawString("欢", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+0*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 61Temp_Graphics.DrawString("迎", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+1*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 62Temp_Graphics.DrawString("使", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+2*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 63Temp_Graphics.DrawString("用", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+3*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 64Temp_Graphics.DrawString("! ", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+4*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 65Temp_Graphics.DrawString("请", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+5*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 66Temp_Graphics.DrawString("按", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+6*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 67Temp_Graphics.DrawString("F5", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+7*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 68Temp_Graphics.DrawString("键", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+8*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 69Temp_Graphics.DrawString("!", new Font(getFont1(),Int(3 * Rnd()+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), int(Font_Size/5+9*1.3*Font_Size), int(image_h/4)+Int(image_h*0.125 * Rnd()+image_h*0.08))'绘出字符 '绘字符的Y方向下波动+4   
 70  
 71dim Temp_Stream as MemoryStream=new MemoryStream()   
 72Temp_Bitmap.Save(Temp_Stream, ImageFormat.jpeg)   
 73Temp_Graphics.Dispose()'释放资源   
 74Temp_Bitmap.Dispose()'释放资源   
 75Temp_Stream.Close()'关闭打开的流文件   
 76return Temp_Stream'返回流   
 77end function 
 78
 79function Show_image(Show_Stream as MemoryStream)'显示内存图像   
 80Response.ClearContent()   
 81Response.ContentType = "Image/Jpeg"   
 82Response.BinaryWrite(Show_Stream.ToArray())   
 83Response.End()   
 84end function 
 85
 86function getChar() as string'获得随机字符 0-9 a-z A-Z   
 87Dim Char_array(5)   
 88Char_array(0) = chr(Int(10 * Rnd()+48))   
 89Char_array(1) = chr(Int(26 * Rnd()+65))   
 90Char_array(2) = chr(Int(26 * Rnd()+65))   
 91Char_array(3) = chr(Int(26 * Rnd()+97))   
 92Char_array(4) = chr(Int(26 * Rnd()+97))   
 93return Char_array(Int(5 * Rnd()))   
 94end function 
 95
 96function getColor() as string'获得随机颜色   
 97Dim int_a As integer   
 98Dim int_b As integer   
 99Dim int_c As integer   
100int_a=Int(180 * Rnd()+20)   
101int_b=Int(180 * Rnd()+20)   
102int_c=Int(180 * Rnd()+20)   
103if int_a>150 and int_b>150 and int_c>150 then int_a=Int(150 * Rnd()+20)   
104return "#" & Hex(int_a) & Hex(int_b) & Hex(int_c)   
105end function 
106
107function getFont() as string'获得随机字体   
108Dim font_array(7)   
109font_array(0) = "Book Antiqua"   
110font_array(1) = "Microsoft Sans Serif"   
111font_array(2) = "Comic Sans MS"   
112font_array(3) = "Comic Sans MS"   
113font_array(4) = "Comic Sans MS"   
114font_array(5) = "Comic Sans MS"   
115font_array(6) = "Comic Sans MS"   
116return font_array(Int(7 * Rnd()))   
117end function 
118
119function getFont1() as string'获得随机字体   
120Dim font_array(10)   
121font_array(0) = "华文行楷"   
122font_array(1) = "隶书"   
123font_array(2) = "华文彩云"   
124font_array(3) = "方正舒体"   
125font_array(4) = "华文彩云"   
126font_array(5) = "方正舒体"   
127font_array(6) = "华文彩云"   
128font_array(7) = "方正舒体"   
129font_array(8) = "楷体_GB2312"   
130font_array(9) = "华文行楷"   
131return font_array(Int(10 * Rnd()))   
132end function   
133'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   
134' 注 意 '   
135' ' '   
136'把所有的代码保存为:ChrToImages.aspx --------->可以直接运行看效果 '   
137'在其它页面的调用方法:<img width=image_w height=image_h src="ChrToImages.aspx" align="absmiddle"> '   
138'image_w、image_h,你要自己算出明确的数值,并注意session("imagenumber") '   
139' '   
140'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    
141</script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus