using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Mis.Pages
{
public class think_test:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private Random rand = new Random();
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = CreateRandomCode(8);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
Web 窗体设计器生成的代码
private string CreateRandomCode(int codeCount)
{
string allChar = "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[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void TransformG(Graphics g)
{
Matrix myMatrix = new Matrix();
int num1=rand.Next(80,900);
int num2=rand.Next(80,800);
float dd1=(float)num1;
float dd2=(float)num2;
dd1=dd1/10000;
dd2=dd2/10000;
float f1 = (float)(dd1);
float f2 = (float)(dd2);
myMatrix.Shear(f1,f2);
g.MultiplyTransform(myMatrix);
}
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(150, 46);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.HighQuality;
Font f = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Regular);
Brush b = new System.Drawing.SolidBrush(Color.LightSlateGray);
Rectangle rect = new Rectangle(0, 0, 150, 46);
HatchBrush hatchBrush = new HatchBrush(
HatchStyle.SmallConfetti,
Color.LightGray,
Color.White);
g.FillRectangle(hatchBrush, rect);
Point[] myArray ={
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46))
};
Pen myPen = new Pen(Color.Blue,1);
GraphicsPath myPath = new GraphicsPath();
myPath.AddBeziers(myArray);
g.DrawPath(myPen, myPath);
SizeF size;
float CPostion=1;
for(int i=0;i<checkCode.Length;i++)
{
TransformG(g);
size = g.MeasureString(checkCode[i].ToString(), f);
g.DrawString(checkCode[i].ToString(), f, b, CPostion, 7);
CPostion+=size.Width-1;
g.ResetTransform();
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
private void Button1_Click(object sender, System.EventArgs e)
{
}
}
}
using System.IO;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Mis.Pages
{
public class think_test:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private Random rand = new Random();
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = CreateRandomCode(8);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
Web 窗体设计器生成的代码
private string CreateRandomCode(int codeCount)
{
string allChar = "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[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void TransformG(Graphics g)
{
Matrix myMatrix = new Matrix();
int num1=rand.Next(80,900);
int num2=rand.Next(80,800);
float dd1=(float)num1;
float dd2=(float)num2;
dd1=dd1/10000;
dd2=dd2/10000;
float f1 = (float)(dd1);
float f2 = (float)(dd2);
myMatrix.Shear(f1,f2);
g.MultiplyTransform(myMatrix);
}
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(150, 46);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.HighQuality;
Font f = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Regular);
Brush b = new System.Drawing.SolidBrush(Color.LightSlateGray);
Rectangle rect = new Rectangle(0, 0, 150, 46);
HatchBrush hatchBrush = new HatchBrush(
HatchStyle.SmallConfetti,
Color.LightGray,
Color.White);
g.FillRectangle(hatchBrush, rect);
Point[] myArray ={
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46)),
new Point(rand.Next(150),rand.Next(46))
};
Pen myPen = new Pen(Color.Blue,1);
GraphicsPath myPath = new GraphicsPath();
myPath.AddBeziers(myArray);
g.DrawPath(myPen, myPath);
SizeF size;
float CPostion=1;
for(int i=0;i<checkCode.Length;i++)
{
TransformG(g);
size = g.MeasureString(checkCode[i].ToString(), f);
g.DrawString(checkCode[i].ToString(), f, b, CPostion, 7);
CPostion+=size.Width-1;
g.ResetTransform();
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
private void Button1_Click(object sender, System.EventArgs e)
{
}
}
}