高手帮忙写个验证码
哪位大侠,能不能给我告诉我验证码是如何做出来的?谢谢…… --------------------编程问答-------------------- http://topic.csdn.net/u/20090429/16/9aee7f0f-16cb-4c49-a789-903aa8143932.html --------------------编程问答--------------------UP --------------------编程问答-------------------- public class ValidCode
{
public ValidCode()
{
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
// HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
/// <summary>
/// 生成验证码图片
/// </summary>
public 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 < 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.Silver), 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);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
public void ValidateCode(string VNum)
{
Bitmap validateimage;
Graphics g;
validateimage = new Bitmap(50, 20, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
g.DrawString(VNum, new Font("Comic Sans MS", 12), new SolidBrush(Color.White), new PointF(3, 0));
g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(0, 0, 0, 0), Color.FromArgb(255, 255, 60, 40)), 0, 0, 120, 30);
g.Save();
MemoryStream ms = new MemoryStream();
validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.End();
}
/// 生成随机数
public string MakeValidateCode()
{
char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
string num = "";
Random r = new Random();
for (int i = 0; i < 4; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}
省的你点击链接了 --------------------编程问答-------------------- 弄个随机数。
------------------------------------------------
【押宝党】 V5
------------------------------------------------
【押宝党】 笔墨红颜 --------------------编程问答--------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="checkCode.aspx.cs" Inherits="checkCode" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
--------------------编程问答-------------------- http://topic.csdn.net/u/20100805/14/05a7620b-beb9-4d30-aeb9-cf5161611733.html 我回复的。拿下来用即可 --------------------编程问答-------------------- 网上很多自己找下 --------------------编程问答-------------------- 都不用百度了,楼上的不少方法就可以直接用 --------------------编程问答-------------------- 我也想看看~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;
public partial class checkCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] str = new string[4];
string serverCode = "";
//生成随机生成器
Random random = new Random();
for (int i = 0; i < 4; i++)
{
str[i] = random.Next(10).ToString().Substring(0, 1);
}
CreateCheckCodeImage(str);
foreach (string s in str)
{
serverCode += s;
}
Session["serverCode"] = serverCode;
}
private void CreateCheckCodeImage(string[] checkCode)
{
if (checkCode == null || checkCode.Length <= 0)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 32.5)), 30);
System.Drawing.Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 20; 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.Silver), x1, y1, x2, y2);
}
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] f = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
for (int k = 0; k <= checkCode.Length - 1; k++)
{
int cindex = random.Next(7);
int findex = random.Next(5);
Font drawFont = new Font(f[findex], 16, (System.Drawing.FontStyle.Bold));
SolidBrush drawBrush = new SolidBrush(c[cindex]);
float x = 5.0F;
float y = 0.0F;
float width = 20.0F;
float height = 25.0F;
int sjx = random.Next(10);
int sjy = random.Next(image.Height - (int)height);
RectangleF drawRect = new RectangleF(x + sjx + (k * 25), y + sjy, width, height);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat);
}
//画图片的前景噪音点
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();
}
}
}
补充:.NET技术 , ASP.NET