抄网上某兄的代码,后面加上了个背景,对背景每个点进行扫描,随机改变点的颜色。
结果性能暴差,做注册单点压力测试的时候,10个用户就不行了。
不知道是不是随机生成点的函数InputBgColorArithmetic影响的性能。gif.aspxusing System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace hnwy
{
/// <summary>
/// gif 的摘要说明。
/// </summary>
public class gif : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string VNum=GenerateCheckCode(4);
Session["VNum"]=VNum;
ValidateCode(VNum);
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// 
/// 生成验证码
/// 
/// 校验码长度
/// 返回校验码
private string GenerateCheckCode(int CodeLength)
{
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(new char[]{','}); //将字符串生成数组
string VNum ="";
Random ro=new Random();   
for(byte i=1;i<=CodeLength;i++)
{
VNum+=VcArray[(int)(35*ro.NextDouble())]; //数组一般从0开始读取,所以这里为35*Rnd
}
VNum="1234";
return VNum;

/// 
/// 将校验码生成图片
/// 
/// 校验码字符串
private void ValidateCode(string VNum)
{
int Gheight=(int)(VNum.Length * 9); //gheight为图片宽度,根据字符长度自动更改图片宽度
System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,18);
Graphics g = Graphics.FromImage(Img);
//刷背景色
Color bgColor=Color.Black;
Rectangle r=new Rectangle(0,0,Gheight,20);
//g.FillRectangle(new SolidBrush(bgColor),r);
InputBgColorArithmetic(g,r,20,Gheight);
//设置字体大小和类型

GraphicsUnit localGraphics=new System.Drawing.GraphicsUnit();
System.Drawing.Font FontPwd=new System.Drawing.Font ("Arial",10,FontStyle.Bold);//,aa); g.DrawString(VNum,FontPwd,new System.Drawing.SolidBrush(Color.Black),0,0); //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
//输出
System.IO.MemoryStream ms=new System.IO.MemoryStream();
//保存
Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType="image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private void InputBgColorArithmetic(Graphics g,Rectangle r,int hight,int longth)
{
//Color bgColor=Color.White;
SolidBrush BrColor=new SolidBrush(Color.Blue);
BrColor.Color=Color.Blue;
Random ro=new Random(10);
int colorstate=0;
for(int i=0;i<longth;i++)
{
for(int j=0;j<hight;j++)
{
if(ro.Next()%9==0)
{
colorstate=ro.Next()%3;
if(colorstate==0)
{
BrColor.Color=Color.Blue;
}
if(colorstate==1)
{
BrColor.Color=Color.Red;
}
if(colorstate==2)
{
BrColor.Color=Color.Green;
}
}
else
{
BrColor.Color=Color.White;
}
g.FillRectangle(BrColor,i,j,1,1);
}
}
}
}
}

解决方案 »

  1.   

    //因为CodeLength一般不会超过8位,太多了难输入,所以采用返回Guid的方式动态生成,最大为8位
    //还可以通过Guid()重载列表进行生成数据的控制,一般默认
    private string GenerateCheckCode(int CodeLength)
    {
          string VNum =string.Entry;
                            Guid guid=Guid.NewGuid();
                            if(CodeLength>8)
                               CodeLength=8;
                            num=guid.ToString().Substring(0,CodeLength);
                   return VNum;

    private void InputBgColorArithmetic(Graphics g,Rectangle r,int hight,int longth)
    {
        //其中的背景我想大可不必通过两个for循环来计算了吧,写个属性进行指定不就可以了吗?
    }