asp.Netビットマップ作成検証ピクチャクラス(検証コードクラス)

1677 ワード

コード:
 
  
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
// ,
using (Image img=new Bitmap(80,25))
{

// , img
using (Graphics g=Graphics.FromImage(img))
{
// ,
g.Clear(Color.White);
// , img.Width-1, img.Height-1
g.DrawRectangle(Pens.Black, 0, 0, img.Width-1, img.Height-1);
// 100 , ,
DrawPoint(100, g, img);
// 4
string vcode=GetCode(4);//vcode Cookie

g.DrawString(vcode,
new Font("Arial", 14, FontStyle.Strikeout | FontStyle.Strikeout), // FontStyle , , |

Brushes.Black,
new RectangleF(r.Next(20), r.Next(7), img.Width, img.Height));
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);// , Jpeg

}
}
}

//

void DrawPoint(int point,Graphics g,Image img)
{
for (int i = 0; i < point; i++)
{
int x = r.Next(img.Width);
int y = r.Next(img.Width);
g.DrawLine(Pens.Red,
new Point(x, y),
new Point(x+2, y+2));

}
}

//
Random r = new Random();

//
string GetCode(int point)
{
string txtStr = "ASF2345WE5R9F3HMBCZ455K";// string char , 1 l , 。
char[] charArr = txtStr.ToArray();
int num = 0;
string code = "";
for (int i = 0; i {
num = r.Next(charArr.Length);
code +=charArr[num];
}
return code;
}