//页面 调用
<div style="float:left; padding-top:1px; padding-left:5px;">
<img src="validitycode.aspx" alt="看不清,换一张" onclick="var r=Math.random();
this.src='/validitycode.aspx?r='+r" style="cursor:pointer;" /></div>
//添加一个validitycode.aspx 后台代码
public partial class validitycode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "no-cache");
Response.AddHeader("Cache-Control", "must-revalidate");
//base.Page_Load(sender, e);
this.CreateCheckCodeImage(GenerateCheckCode());
}
protected string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
{
do
{
number = random.Next();
if (number % 2 == 0)
code = (char)('1' + (char)(number % 9));
else
code = (char)('A' + (char)(number % 26));
} while (checkCode.Contains(code.ToString()));
checkCode += code.ToString();
}
SessionManager.Identitycode = 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 * 15.0)), 25);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.FromArgb(166, 255, 186));
////画图片的背景噪音线
//for (int i = 0; i < 25; 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.DarkGray), x1, y1, x2, y2);
//}
Color forecolor1 = Color.FromArgb(255, 0, 0);
Color forecolor2 = Color.FromArgb(0, 0, 255);
Font font = new System.Drawing.Font("Arial", 14F, (System.Drawing.FontStyle.Regular | 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);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), forecolor1, forecolor2, 1.2f, true);
g.DrawString(checkCode, font, brush, 0, 1);
//Random random1 = new Random();
//image = TwistImage(image, true, (double)random1.Next(-30, 30) / 10, (double)random1.Next(-30, 30) / 10);//random1.Next(10, 23), random1.Next(1, 8));//22.33, 0.03);
////画图片的前景噪音点
//for (int i = 0; i < 50; 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();
}
}
/// <summary>
/// 正弦曲线Wave扭曲图片
/// </summary>
/// <param name="srcBmp"></param>
/// <param name="bXDir"></param>
/// <param name="nMultValue">波形的幅度倍数</param>
/// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
/// <returns></returns>
private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
double PI = 6.283185307179586476925286766559;
Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
Graphics graph = Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
{
for (int j = 0; j < destBmp.Height; j++)
{
double dx = 0;
dx = bXDir ? (PI * (double)j) / dBaseAxisLen : (PI * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
srcBmp.Dispose();
return destBmp;
}
}