抄网上某兄的代码,后面加上了个背景,对背景每个点进行扫描,随机改变点的颜色。
结果性能暴差,做注册单点压力测试的时候,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);
}
}
}
}
}
解决方案 »
- 如何将SqlDataSource1赋给DataTable上?
- asp.net文章分页,可以手动,可以自动分页的做法,
- 看看这个写成类该怎么写?
- 关于DataGrid显示列的问题~!
- 小小问题.请赐教
- 急:treeview: 我给一个存储node 的ID的数组,有什么最简便高效的遍历方法来实现 使ID值存在于这个数组中的node 展开并checked被选中?
- 那边没人回我这边问:弹出窗口技术,你熟悉吗?
- 帮忙把c#的代码转换为vb.net 在线等!!!
- 框架问题急~~~~~~~
- 如何做网页的刷新问题
- 如何使数据提交后,弹出询问框(询问继续添加记录,或返回)
- 如何處理asp.net中的用戶交互過程
//还可以通过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循环来计算了吧,写个属性进行指定不就可以了吗?
}