c#生成图片验证码代码_.Net教程
教程Tag:暂无Tag,欢迎添加,赚取U币!
推荐:Asp.Net生成静态页面实例代码usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Collections; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Web.UI.H
前台引用.aspx:<img id="Image1" onclick="javascript:suiji();"src="YanZhengCode.aspx" alt="看不清楚,请单击我。" border="0"style="height:25px;width:60px;border: 0px; cursor: pointer;" /> <script language="javascript" type="text/javascript"> function suiji(){ document.getElementById("Image1").src="gif_zl.aspx?chen="+number; } </script> |
YanZhengCode.cs
using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.IO; public partial class YanZhengCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //string str = Request["chen"]; BuildCode(); //ShuImg(); } private void BuildCode() { Color[] strcolor ={ Color.Red, Color.Blue, Color.Brown,Color.Black,Color.Tomato }; string[] strfont={"华文中宋"}; Random rands = new Random(); Bitmap images = new Bitmap(80,26); //图片大小 Graphics g = Graphics.FromImage(images); Font f = new Font(strfont[0], 20); //字体大小 //笔刷填充色 Brush b = new SolidBrush(Color.Tomato); Brush b2 = new SolidBrush(strcolor[rands.Next(0,strcolor.Length)]); Brush b3 = new SolidBrush(Color.Red); Brush b4 = new SolidBrush(Color.FromArgb(128,Color.Green)); g.Clear(Color.Wheat); //线条颜色 Pen p = new Pen(Color.Red, 0); Pen p2 = new Pen(Color.Turquoise,0); Random rand = new Random(); for (int i = 0; i < 40; i++) { int y = rand.Next(images.Height); int x = rand.Next(images.Width); //g.DrawLine(p, 0, y, images.Width, y); g.DrawRectangle(p2, x, y, 1, 1); } //int degree = 5; //Point tempPoint = new Point(); //degree = ~degree + 1; //g.RotateTransform(degree); //tempPoint.X = 20; //tempPoint.Y = 4; g.DrawRectangle(p, 0, 0, images.Width-1, images.Height-1); //绘图区域 string strCode = rand.Next(1000,10000).ToString(); Session["yanzhencode"] = strCode; g.DrawString(strCode.Substring(0, 1), f, b, 2,-1); g.DrawString(strCode.Substring(1, 1), f, b2, 19, -1); g.DrawString(strCode.Substring(2, 1), f, b3, 37, -1); g.DrawString(strCode.Substring(3, 1), f, b4, 50, -1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); images.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/jpeg"; Response.BinaryWrite(ms.ToArray()); //Response.Cookies.Add(new HttpCookie("chne", "6780")); g.Dispose(); images.Dispose(); } private void ShuImg() { Response.ContentType = "image/*"; //设置发送头为图像数据 Response.Clear(); //清空发送数据 Random rand = new Random(); Stream stream = OpenFile(Server.MapPath("pic/"+rand.Next(1,6).ToString()+".jpg")); //打开文件流 Bitmap bit = new Bitmap(stream); bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//保存到输出流中 stream.Close(); bit.Dispose(); Response.Flush(); //输出到客户端 } } |
分享: 解析防网站登陆被破解的简单方法在大多数的基于数据库的身份认证登陆模块,大多数的程序员只是用一个简单的SQL查询语句来实现,这样很容易被用户以简单的 (1’or’1’=’1) 查询替换给破解.其实只要稍微的修改一下代码,便可以防止.具体请参看以下两个函数的实现: 以下代码基于C#,数据库为Acce
相关.Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP发送Email实例(可带附件)
- js实现广告漂浮效果的小例子
- asp.net Repeater 数据绑定的具体实现
- Asp.Net 无刷新文件上传并显示进度条的实现方法及思路
- Asp.net获取客户端IP常见代码存在的伪造IP问题探讨
- VS2010 水晶报表的使用方法
- ASP.NET中操作SQL数据库(连接字符串的配置及获取)
- asp.net页面传值测试实例代码
- DataGridView - DataGridViewCheckBoxCell的使用介绍
- asp.net中javascript的引用(直接引入和间接引入)
- 三层+存储过程实现分页示例代码
- 相关链接:
- 教程说明:
.Net教程-c#生成图片验证码代码。