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.IO;
using System.Drawing;
using System.Security;
public partial class Picture : System.Web.UI.Page
{
Random rm = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = GetRamdomValidate(4);
Session["check"] = str;//将验证码写入Session,进行验证,也可以使用cookie
GetImageValidate(str);
}
//得到随机字符串,长度自己定义
private string GetRamdomValidate(int len)
{
int num;
int tem;
string rtuStr = "";
for (int i = 0; i < len; i++)
{
num = rm.Next();
//生成数字与字母组合的验证码
if (num % 2 == 0)
tem = num % 10 + '0';
else
tem = num % 26 + 'A';
/*用此方式会生成数字*/
// tem = num % 10 + '0';
rtuStr += Convert.ToChar(tem).ToString();
}
return rtuStr;
}
//生成图像
private void GetImageValidate(string strValue)
{
//int width = "OO00";
int width = Convert.ToInt32(strValue.Length * 12);//计算图像宽度
Bitmap img = new Bitmap(width, 23);
Graphics gfc = Graphics.FromImage(img); //产生Graphic对象,进行画图
gfc.Clear(Color.White);
drawLine(gfc, img);
//写验证码,需要定义Font
Font font = new Font("arial", 12, FontStyle.Bold|FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width,
img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
gfc.DrawString(strValue, font, brush, 3, 2);
drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
//将图像添加到页面
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//更改HTTP头
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
//Dipose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawLine(Graphics gfc, Bitmap img)
{
for (int i = 0; i < 10; i++)
{
int x1 = rm.Next(img.Width);
int y1 = rm.Next(img.Height);
int x2 = rm.Next(img.Width);
int y2 = rm.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);//注意画笔一定要浅颜色,否则验证码看不清楚
}
}
private void drawPoint(Bitmap img)
{
int col = rm.Next();
for (int i = 0; i < 100; i++)
{
int x = rm.Next(img.Width);
int y = rm.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(col));//Color.FromArgb(rm.Next())生成随机色
}
}
}
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.IO;
using System.Drawing;
using System.Security;
public partial class Picture : System.Web.UI.Page
{
Random rm = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = GetRamdomValidate(4);
Session["check"] = str;//将验证码写入Session,进行验证,也可以使用cookie
GetImageValidate(str);
}
//得到随机字符串,长度自己定义
private string GetRamdomValidate(int len)
{
int num;
int tem;
string rtuStr = "";
for (int i = 0; i < len; i++)
{
num = rm.Next();
//生成数字与字母组合的验证码
if (num % 2 == 0)
tem = num % 10 + '0';
else
tem = num % 26 + 'A';
/*用此方式会生成数字*/
// tem = num % 10 + '0';
rtuStr += Convert.ToChar(tem).ToString();
}
return rtuStr;
}
//生成图像
private void GetImageValidate(string strValue)
{
//int width = "OO00";
int width = Convert.ToInt32(strValue.Length * 12);//计算图像宽度
Bitmap img = new Bitmap(width, 23);
Graphics gfc = Graphics.FromImage(img); //产生Graphic对象,进行画图
gfc.Clear(Color.White);
drawLine(gfc, img);
//写验证码,需要定义Font
Font font = new Font("arial", 12, FontStyle.Bold|FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width,
img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
gfc.DrawString(strValue, font, brush, 3, 2);
drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
//将图像添加到页面
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//更改HTTP头
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
//Dipose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawLine(Graphics gfc, Bitmap img)
{
for (int i = 0; i < 10; i++)
{
int x1 = rm.Next(img.Width);
int y1 = rm.Next(img.Height);
int x2 = rm.Next(img.Width);
int y2 = rm.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);//注意画笔一定要浅颜色,否则验证码看不清楚
}
}
private void drawPoint(Bitmap img)
{
int col = rm.Next();
for (int i = 0; i < 100; i++)
{
int x = rm.Next(img.Width);
int y = rm.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(col));//Color.FromArgb(rm.Next())生成随机色
}
}
}
511

被折叠的 条评论
为什么被折叠?



