private MemoryStream GetByValidateCode(string VNum)//返回内存流
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
Random random = new Random();
int gheight = VNum.Length * 12;
Img = new Bitmap(gheight, 20);
g = Graphics.FromImage(Img);
Font f = new Font("Arial", 12, FontStyle.Bold);
g.Clear(GetByRandColor(180, 200));//设定背景色
Pen blackPen = new Pen(Color.AntiqueWhite, 1);
//Pen blackPen = new Pen(Color.Black ,1);
for (int i = 0; i < 32; i++)// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
{
int x = random.Next(gheight);
int y = random.Next(20);
int xl = random.Next(6);
int yl = random.Next(12);
g.DrawLine(blackPen, x, y, x + xl, y + yl);
}
SolidBrush s = new SolidBrush(Color.SaddleBrown);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//Jpeg改Png即可
g.Dispose();
Img.Dispose();
return ms;
}
楼上的兄弟比较强
学学
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class validate : System.Web.UI.Page
{
private const int rndLength = 4;
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
string vnum;
vnum = GetByRndNum();
Session["validatecode"] = vnum;
private MemoryStream GetByValidateCode(string VNum)//返回内存流
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
Random random = new Random();
int gheight = VNum.Length * 12;
Img = new Bitmap(gheight, 20);
g = Graphics.FromImage(Img);
Font f = new Font("Arial", 12, FontStyle.Bold);
g.Clear(GetByRandColor(180, 200));//设定背景色
Pen blackPen = new Pen(Color.AntiqueWhite, 1);
//Pen blackPen = new Pen(Color.Black ,1);
for (int i = 0; i < 32; i++)// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
{
int x = random.Next(gheight);
int y = random.Next(20);
int xl = random.Next(6);
int yl = random.Next(12);
g.DrawLine(blackPen, x, y, x + xl, y + yl);
}
SolidBrush s = new SolidBrush(Color.SaddleBrown);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//Jpeg改Png即可
g.Dispose();
Img.Dispose();
return ms;
}
//-----------------给定范围获得随机颜色------------
Color GetByRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
//if(ac>255) ac=255;
int r = fc + random.Next(bc - fc);
int g = fc + random.Next(bc - fc);
int b = fc + random.Next(bc - bc);
Color rs = Color.FromArgb(r, g, b);
return rs;
}
//-----------------------取随机产生的认证码(N位数字)
public string GetByRndNum()
{
string Vchar = "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,w,x,y,z" ;
string[] VcArray = Vchar.Split(',');
string VNum = "";
//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
int[] rndArr = new int[rndLength];
for (int i = 1; i < rndLength + 1; i++)
{
if(i>1)
{
do
{
rndArr[i-1] = rand.Next(10);//rand.Next(35) ;
bool goon = false;
for(int ix=0; ix<i-1; ix++)
{
if (rndArr[ix] == rndArr[i-1])
{
goon = true;
break;
}
}
if (!goon)
break;
} while (1 == 1);
}
else
rndArr[i-1] = rand.Next(10);
VNum += rndArr[i-1];
}
return VNum;
}
}