Asp.Net验证码2

时间:2023-06-30 16:42:56
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging; public partial class user_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateRandomCode();
GetImgWithValidateCode(checkCode, , ,);
}
///<summary>
/// 生成随机字符串
///</summary>
///<param name="codeCount">字符数量</param>
///<returns>随机字符串</returns>
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -;
Random rand = new Random();
for (int i = ; i < codeCount; i++)
{
if (temp != -)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next();
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
} ///<summary>
/// 创建验证码图片
///</summary>
///<param name="code">字符串</param>
///<param name="width">宽</param>
///<param name="height">高</param>
///<param name="fontSize">字号</param>
public void GetImgWithValidateCode(string code, int width, int height, int fontSize)
{
Color bgcolor = GetControllableColor();
Color color = GetControllableColor(); int charNum = code.Length;
Bitmap bitMap = null;
Graphics gph = null;
//创建内存流
MemoryStream memStream = new MemoryStream();
Random random = new Random(); //创建位图对象
bitMap = new Bitmap(width, height);
//根据上面创建的位图对象创建绘图图面
gph = Graphics.FromImage(bitMap);
//设定验证码图片背景色
gph.Clear(bgcolor);
//产生随机干扰线条
for (int i = ; i < ; i++)
{
Pen backPen = new Pen(color, );
int x = ;
int y = height / ;
int x2 = width;
int y2 = random.Next(height);
gph.DrawLine(backPen, x, y, x2, y2);
} SolidBrush sb = new SolidBrush(color);
PointF Cpoint1 = new PointF(, );
Random rnd1 = new Random();
int x1 = , y1 = ;
//通过循环,绘制每个字符,
for (int i = ; i < code.Length; i++)
{
x1 = rnd1.Next() + ((width - ) / code.Length) * i;
y1 = rnd1.Next(bitMap.Height / );
Cpoint1 = new PointF(x1, y1);
Font textFont = new Font("Arial", fontSize, FontStyle.Bold);//字体随机,字号大小30,加粗 //随机倾斜字符
Matrix transform = gph.Transform;
transform.Shear(Convert.ToSingle(rnd1.NextDouble() - 0.5), 0.001f);
gph.Transform = transform;
gph.DrawString(code.Substring(i, ), textFont, sb, Cpoint1);
gph.ResetTransform();
} //画图片的前景噪音点
for (int i = ; i < ; i++)
{
int x = random.Next(bitMap.Width);
int y = random.Next(bitMap.Height);
bitMap.SetPixel(x, y, Color.White);
}
//画图片的边框线
gph.DrawRectangle(new Pen(Color.Black, ), , , bitMap.Width - , bitMap.Height - );
try
{
bitMap.Save(memStream, ImageFormat.Gif);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
bitMap.Dispose();
System.Drawing.Image img = System.Drawing.Image.FromStream(memStream); gph.DrawImage(img, , , width, height);
//img.Save(@"D:\test.jpg", ImageFormat.Jpeg); //如果是asp.net 用下面的输出图片 System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
gph.Dispose();
img.Dispose();
} ///<summary>
/// 产生一种 R,G,B 均大于 colorBase 随机颜色,以确保颜色不会过深
///</summary>
///<param name="colorBase">The color base.</param>
///<returns>Color</returns>
public Color GetControllableColor(int colorBase)
{
Color color = Color.Black;
if (colorBase > )
{
return color;
}
Random random = new Random();
color = Color.FromArgb(random.Next() + colorBase, random.Next() + colorBase, random.Next() + colorBase);
return color;
}
}

验证码效果:

Asp.Net验证码2

还有2种验证码请看我博客

http://www.cnblogs.com/webapi/p/5725576.html

http://www.cnblogs.com/webapi/p/5726498.html