Asp.net源码专业站
首页->新知实践->简单四则运算验证码(C#)源码>>ValidationCode/Image.aspx.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:简单四则运算验证码(C#)源码
当前文件:文件类型 SiZeYunSuanYanZhengMa/ValidationCode/Image.aspx.cs[3K,2009-6-12 11:54:19]打开代码结构图
普通视图
		            
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11 12using System.Drawing; 13using System.Drawing.Imaging; 14using System.Drawing.Drawing2D; 15 16namespace ValidationCode 17{ 18 19 public partial class Image : System.Web.UI.Page 20 { 21 protected void Page_Load(object sender, EventArgs e) 22 { 23 Random rd = new Random(); //创建随机数对象 24 25 int Results = 0; 26 int Num1 = rd.Next(10); ; 27 int Num2 = rd.Next(10); ; 28 string Expressions = ""; 29 30 int F=(rd.Next(4)+1); 31 switch (F) 32 { 33 case 1: 34 Results = Num1 + Num2; 35 Expressions = Num1 + "" + Num2 + "" + Results; 36 break; 37 case 2: 38 Results = Num1 - Num2; 39 Expressions = Num1 + "" + Num2 + "" + Results; 40 break; 41 case 3: 42 Results = Num1 * Num2; 43 Expressions = Num1 + "×" + Num2 + "" + Results; 44 break; 45 case 4: 46 if (Num2 > 0) 47 { 48 Results = Convert.ToInt16(Num1 / Num2); 49 Expressions = Num1 + "整除 " + Num2 + "" + Results; 50 } 51 else 52 { 53 Results = Num1; 54 Expressions = Num1 + "整除 1= " + Results; 55 } 56 break; 57 } 58 59 //验证码值存放到Session中用来比较 60 Session["Code"] = Results.ToString(); 61 62 //以下通过随机找一个现有图象产生一个画布Bitmap 63 //随机找个背景 64 string bgFilePath = Server.MapPath(".\\images\\bg" + new Random().Next(5) + ".jpg"); 65 //string bgFilePath = Server.MapPath(".\\images\\bg10.jpg"); 66 67 68 System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath); 69 //建立位图对象 70 Bitmap newBitmap = new Bitmap(imgObj, 290, 80); 71 72 //根据上面创建的位图对象创建绘图面 73 Graphics g = Graphics.FromImage(newBitmap); 74 //设置画笔颜色 75 SolidBrush brush = new SolidBrush(Color.Blue); 76 77 //定义一个含10种字体的数组 78 String[] fontFamily ={ "Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler", "Lucida Sans Unicode", "Garamond", "Courier New", "Book Antiqua", "Arial Narrow" }; 79 80 81 //通过循环,绘制每个字符, 82 for (int a = 0; a < Expressions.Length; a++) 83 { 84 //字体随机,字号大小30,加粗 85 Font textFont = new Font(fontFamily[rd.Next(9)],20, FontStyle.Bold); 86 //每次循环绘制一个字符,设置字体格式,画笔颜色,字符相对画布的X坐标,字符相对画布的Y坐标 87 g.DrawString(Expressions.Substring(a, 1), textFont, brush, 20 + a * 22, 25); 88 } 89 90 //保存画的图片到输出流中 91 newBitmap.Save(Response.OutputStream, ImageFormat.Gif); 92 93 } 94 } 95} 96
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:简单四则运算验证码(C#)源码
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146