using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
namespace WebApplication7
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
createCheckImage(checkcode());
}
public string checkcode()
{
string result = string.Empty;
char[] resoure = new char[] { '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', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'D', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
int length = 4;
System.Random random = new Random();
for (int i = 0; i < length; i++)
{
char t=resoure[random.Next(resoure.Length)];
result += t;
}
return result;
}
public void createCheckImage(string code)
{
int wid = 80;
int len = 40;
System.Random random = new Random();
System.Drawing.Bitmap image = new Bitmap(wid, len);
System.Drawing.Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(wid);
int y1 = random.Next(len);
int x2 = random.Next(wid);
int y2 = random.Next(len);
g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);
}
Font font=new Font("Arial",20);
int x=0,y=0;
for (int i = 0; i < code.Length; i++)
{
char c = code[i];
g.DrawString(c.ToString(), font, new SolidBrush(Color.Black), x, y);
x += 17;
// y += 10;
}
g.DrawRectangle(new Pen(new SolidBrush(Color.Black)), 0,0,wid-1,len-1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
}
asp.net 创建验证码
最新推荐文章于 2024-02-20 23:57:02 发布