}
set
{
pTempImageURLPath = value;
}
}
[Bindable(true),Browsable(true),Category("Appearance"),DefaultValue(GraphicType.Jpg),Description("选择生成校验图文件的类型(Jpg;Gif;Png;Bmp)!")]
public GraphicType ChartType
{
get
{
return pChartType;
}
set
{
pChartType = value;
}
}
//生成校验码图片的宽度
public int ChartWidth
{
get
{
return pChartWidth;
}
set
{
pChartWidth = value;
}
}
//生成校验码图片的高度
public int ChartHeight
{
get
{
return pChartHeight;
}
set
{
pChartHeight = value;
}
}
//需要生成的校验码
public string AuthenCode
{
get
{
return pAuthenCode;
}
set
{
pAuthenCode = value;
}
}
#endregion
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
System.Web.UI.WebControls.Image objImage;
//TextBox objTxt;
//绘制包含的控件
objImage = (System.Web.UI.WebControls.Image) Controls[0];
//objTxt = (TextBox) Controls;
if(pAuthenCode==null)
pAuthenCode=GetValidateCode();
OnGraphicOK(this,EventArgs.Empty );
GetRandomImage(pAuthenCode);
objImage.ImageUrl=pAuthenImageFullURL;
objImage.RenderControl(output);
}
/// <summary>
/// 给控件添加子控件
/// </summary>
protected override void CreateChildControls( )
{
//Controls.Add(btn);
Controls.Add(img);
//Controls.Add(txt);
}
/// <summary>
/// 控件Load时候属性的初始化
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(System.EventArgs e)
{
EraseOldGraphic(); //删除过期的图片
}
/// <summary>
/// 生成随机的
/// </summary>
private void MakeRandomFileName()
{
string strRandName=DateTime.Now.Ticks.ToString()+".jpg";
pAuthenImageFullname=this.Page.MapPath(TempImageURLPath)+@"\"+strRandName;
pAuthenImageFullURL=TempImageURLPath+"/"+strRandName;
}
private void GetRandomImage(string strValidateCode)
{
//生成随即图片的全名,和全URL
MakeRandomFileName();
validateImage = new Bitmap(pChartWidth, pChartHeight, PixelFormat.Format24bppRgb); // .Format24bppRgb);
g = Graphics.FromImage(validateImage);
g.Clear(Color.LightGray) ;
//g.DrawString(strValidateCode , new Font("宋体",16,FontStyle.Bold),new SolidBrush(Color.DarkRed),new PointF(2,2));
for(int i=0;i<strValidateCode.Length;i++)
{
Random r = new Random();
PointF startPos=new PointF(r.Next(3,6)+(r.Next(12,14)*i ),r.Next(-1,2) );
g.DrawString(strValidateCode.Substring(i,1) , new Font("宋体",14,FontStyle.Italic),new SolidBrush(Color.Blue),startPos);
}
//g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(120,30), Color.F