C# Captcha 测试 firefox 和 IE

为了防止 对方用代码 瘫痪 注册页面, 有时要用到验证码识别 人还是代码 在填写你的网页。
Step1:  创建一个web 应用
Step2:  进入创建的应用的后部代码
Step3:  添加 Namespace for drawing Image.
using System.Drawing;
using System.Drawing.Imaging;
Step4: // 定义随机产生4个数字

ContractedBlock.gif ExpandedBlockStart.gif Code
public static string GetRandomPassword()
{
Random rand 
= new Random();
System.Text.StringBuilder password 
= new System.Text.StringBuilder(4);
for (int i = 1; i <= length; i++

int charIndex;  

do { charIndex = rand.Next(48123); } 

while (!(charIndex >= 48 && charIndex <= 57)); 

password.Append(Convert.ToChar(charIndex)); } 

return password.ToString(); } 

 

Step5: //把上面的数字转换成图片
ContractedBlock.gif ExpandedBlockStart.gif Code
 1 private void GenerateImage(string strRegistrationStr)
 2 {
 3 Bitmap objBitmap = new Bitmap(15030);
 4 Graphics objGraphics = Graphics.FromImage(objBitmap);
 5 SolidBrush objForeColor = new SolidBrush(Color.White);
 6 SolidBrush objBackColor = new SolidBrush(Color.Black);
 7 objGraphics.FillRectangle(objBackColor, 0015030);
 8 Font objFont = new Font("Arial"15);
 9 Point objPoint = new Point(55);
10 objGraphics.DrawString(strRegistrationStr, objFont, objForeColor, objPoint);
11 //保存图片
12 //或直接不保存objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
13 objBitmap.Save(Server.MapPath("zhuchetu.gif"), ImageFormat.Gif);
14 if(objBitmap != null)
15 objBitmap.Dispose();
16 if (objGraphics != null)
17 objGraphics.Dispose();
18 }
19 
 

Step6: 最后代码调用这个

ContractedBlock.gif ExpandedBlockStart.gif Code
protected void btnzhutu_Click(object sender, EventArgs e)
{
GenerateImage(GetRandomPassword());
ImgRegistrationStr.ImageUrl 
= "zhuchetu.gif";
}

 

 

转载于:https://www.cnblogs.com/sandy_liao/archive/2008/12/05/1348653.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值