using System;
using System.Data;
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;
public partial class checkcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(GenerateCheckCode());
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 2; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
分享到:
相关推荐
C#图片验证码字母或数字通用识别代码,各种图片验证码都可以识别。识别速度快速,无需等待,立即返回。C#图片验证码字母或数字通用识别代码
C#图片验证码识别技术 C#图片验证码识别技术 C#图片验证码识别技术 C#图片验证码识别技术 C#图片验证码识别技术 C#图片验证码识别技术 C#图片验证码...
C#验证码 验证码绘制在图片上 很不错
通过网上的一些资料结合,用c#写的验证码识别器,里面有代码,
public void imgdo(Bitmap img) { //去色 Bitmap btp = img; Color c = new Color(); int rr, gg, bb; for (int i = 0; i < btp.Width;... //取图片当前的像素点 c = btp.GetPixel(i, j); rr = c.
2009-7-12 制作完成,支持3种模式:数字、数字+字母、汉字,多页面同时产生验证码而不冲突,防御能力还行。演示地址:http://www.netdust.cn/code/cs/example.aspx,我的下载:...
本程序是用c#实现的,用于识别普通图片验证码,通过图像处理技术获取验证码。
共享自己用c#写的,带加减法的验证码源代码
C#源码图片验证码识别,代码有详细说明,是学习验证码识别的一个不错的参考示例。
C#实现验证码识别、发票标号识别、图片识别.zip
1 :产生一个验证码的图片(当能这个是必须的) 2 : 图片中的 重要包括3部分,字符,起干扰作用的线条,起干扰作用的小圆 3 :如下都是随机产生的 ① 文字的颜色,线条的颜色,小圆的颜色 ② 文字的倾斜角度 ③ 线条的...
C#汉字验证码,使用了GB2312字库中的汉字,随机生成汉字图片
C# 生成随即验证码图片类库,其中可选择验证码类型(数字,字母,汉字,组合式),长度,保存类型(Session,Cookie)等
c#生成图形验证码 --一图片的形式生成验证码
.NETCore图形验证码,包含滑动验证和图片文字点击,适合简单登录验证用。包含滑动验证和图片文字点击,适合简单登录验证用。包含滑动验证和图片文字点击,适合简单登录验证用。包含滑动验证和图片文字点击,适合简单...
C#获取验证码图片 添加MSHTML引用 添加MSHTML引用 添加MSHTML引用
c#生成图片验证码代码,简单实用,随手笔记。。。
C#验证码识别参考算法
C# mvc实现的点选图片验证码,图片在服务端生成,服务端校验.一部分代码也是参考了网上开源的资料.自己完善后共享给大家.