using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class yanzm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCodeString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int length = checkCodeString.Length;
Font font = new Font("宋体", 14, FontStyle.Bold);
Brush brush = null;
Color brushColor = new Color();
string checkCode = string.Empty;
string code = string.Empty;
Bitmap image = new Bitmap(65, 22);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
Random random=new Random();
for (int i = 0; i < 4; i++)
{
int current = random.Next(DateTime.Now.Millisecond) % length;
code = checkCodeString.Substring(current, 1);
checkCode = checkCode + code;
brushColor = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255));
brush = new SolidBrush(brushColor);
g.DrawString(code, font, brush, i * 15 + 2, 2);
}
//随机产生的线条
LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Red, 1.2f, true);
for (int i = 0; i < 1; 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);
}
//背景
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(image.Width);
int y1 = random.Next(image.Height);
image.SetPixel(x1, y1, Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
Response.Clear();
Response.ContentType = "image/pjpeg";
image.Save(Response.OutputStream, ImageFormat.Jpeg);
Session["checkCode"] = checkCode;
image.Dispose();
Response.End();
}
}
<asp"Image ID="Image1" runat="Server" ImageUrl="~/yanzm.aspx">
<a href="#" οnclick="document.getElementById('Image1').src='yanzm.aspx?id=+'Math.random()">看不清,换一张</a>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<img alt="图片看不清?点击重新得到验证码" src="CheckCode.aspx?k=" onClick="this.src+=Math.random()" />
string checkCode = Session["CheckCode"] as string;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// 生成验证码的页面,注意这个页面的设计代码中没有HTML代码
/// </summary>
public partial class CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string codeSource = "0123456789abcdefg";//定义校验码中的验证字符集
int width = 48;
int height = 48;
Random random = new Random();
string code = "";
int number = 0;
for (int i = 0; i < 4; i++)
{
number = random.Next(DateTime.Now.Millisecond) % codeSource.Length;
code += codeSource.Substring(number, 1);
}
Bitmap image = new Bitmap(48, 24);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
float emSize = (float)width / code.Length;
Font font = new Font("Arial", emSize, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
Pen pen = new Pen(Color.Silver);
#region 画图片的背景噪音线
int x1, y1, x2, y2;
for (int i = 0; i < 25; i++)
{
x1 = random.Next(image.Width);
y1 = random.Next(image.Height);
x2 = random.Next(image.Width);
y2 = random.Next(image.Height);
g.DrawLine(pen, x1, y1, x2, y2);
}
#endregion
#region 画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
x1 = random.Next(image.Width);
y1 = random.Next(image.Height);
image.SetPixel(x1, y1, Color.FromArgb(random.Next(Int32.MaxValue)));
}
#endregion
g.DrawString(code, font, Brushes.Red, 2, 2);
g.Dispose();
Session["CheckCode"] = code;
image.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}