对生成的验证码图片进行了随机处理,加大了破解难度。

但还没解决设计时支持的问题。不知为何?
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;

namespace Common.Web.AppControl


{

/**//// <summary>
/// 效验码控件
/// </summary>
/// <summary>
/// 上传进度条控件
/// </summary>
/// <example>
/// Web.config 配置
/// <?xml version="1.0"?>
///<configuration>
/// <configSections>
/// <sectionGroup name="dataGroup">
/// <!--加密算法-->
/// <section name="encrypt" type="Common.Framework.ProviderConfigurationHandler, Common"/>
/// </sectionGroup>
/// </configSections>
/// <dataGroup>
/// <encrypt defaultProvider="EncryptRe">
/// <providers>
/// <clear/>
/// <!--可逆-->
/// <add name="EncryptRe" type="Common.DEncrypt.EncryptProvider, Common" classname="Common.DEncrypt.EncryptRe, Common"/>
/// <!--不可逆-->
/// <add name="EncryptUnRe" type="Common.DEncrypt.EncryptProvider, Common" classname="Common.DEncrypt.EncryptUnRe, Common"/>
/// </providers>
/// </encrypt>
/// </dataGroup>
/// <system.web>
/// <httpHandlers>
/// <add verb="*" path="CommonToolsASHX.ashx" type="Common.Web.AppControl.CommonToolsASHX, Common" />
/// </httpHandlers>
/// </system.web>
///</configuration>
/// </example>
[Designer(typeof(WebCheckCode.Designer))]
[ToolboxData("<{0}:WebCheckCode runat=server></{0}:WebCheckCode>")]
public class WebCheckCode : Control, INamingContainer

{

WebCheckCode#region WebCheckCode

private#region private
private string checkwordNow

{
get

{
return "CheckWordNow" + this.UniqueID;
}
}
private string checkwordLength

{
get

{
return "CheckWordLength" + this.UniqueID;
}
}
private string checkwordType

{
get

{
return "CheckWordType" + this.UniqueID;
}
}

private string imgBuildFileUrl

{
get

{
return "ImgBuildFileUrl" + this.UniqueID;
}
}
private string imgCss

{
get

{
return "imgCss" + this.UniqueID;
}
}
private string GetRandomWord()

{
Common.StrHelp.Random random = new Common.StrHelp.Random(this.Length);
return random.GetRandomWord(this.WordType);
}

private string _CheckWordNew = null;
private string CheckWordNew

{
get

{
if (this._CheckWordNew == null)

{
this._CheckWordNew = Common.DEncrypt.Factory.GetInstanceRe().EncryptString(this.GetRandomWord());
}
return this._CheckWordNew;
}
}
private string _CheckWordNow;
protected string _ImgBuildFileUrl = Common.Web.UrlInfo.Url_CommonToolsASHX;
private string _ImgCss = null;
private Common.StrHelp.RandomWordType _WordType = Common.StrHelp.RandomWordType.Num;
private int _CheckWorkLength = 4;
#endregion


属性#region 属性


Value 效验码值#region Value 效验码值

/**//// <summary>
/// 效验码值
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string Value

{
get

{
return Common.DEncrypt.Factory.GetInstanceRe().DecryptString(this._CheckWordNow);
}
}
#endregion


WordType 控件字符串类型#region WordType 控件字符串类型

/**//// <summary>
/// 控件字符串类型 WordType
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public Common.StrHelp.RandomWordType WordType

{
get

{
return this._WordType;
}
set

{
this._WordType = value;
}
}
#endregion


Length 效验码长度#region Length 效验码长度

/**//// <summary>
/// 效验码长度
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("4")]
[Localizable(true)]
public int Length

{
get

{
return this._CheckWorkLength;
}
set

{
this._CheckWorkLength = value;
}
}
#endregion


ImgBuildFileUrl 输出图片的地址#region ImgBuildFileUrl 输出图片的地址

/**//// <summary>
/// 输出图片的地址
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
[UrlProperty]
public virtual string ImgBuildFileUrl

{
get

{
return this._ImgBuildFileUrl;
}
set

{
this._ImgBuildFileUrl = value;
}
}
#endregion


ImgCss 图片样式#region ImgCss 图片样式

/**//// <summary>
/// 图片样式
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[Localizable(true)]
public string ImgCss

{
get

{
return this._ImgCss;
}
set

{
this._ImgCss = value;
}
}
#endregion

#endregion


override#region override
protected override object SaveViewState()

{
System.Collections.Specialized.ListDictionary ld = new System.Collections.Specialized.ListDictionary();
ld.Add(checkwordType, this._WordType);
ld.Add(checkwordLength, this._CheckWorkLength);
ld.Add(checkwordNow, this.CheckWordNew);
ld.Add(imgBuildFileUrl, this._ImgBuildFileUrl);
ld.Add(imgCss, this._ImgCss);
return ld;
}

protected override void LoadViewState(object savedState)

{
if (savedState != null)

{
System.Collections.Specialized.ListDictionary ld = (System.Collections.Specialized.ListDictionary)savedState;
this._WordType = (Common.StrHelp.RandomWordType)ld[checkwordType];
this._CheckWorkLength = (int)ld[checkwordLength];
this._CheckWordNow = (string)ld[checkwordNow];
this._ImgBuildFileUrl = (string)ld[imgBuildFileUrl];
this._ImgCss = (string)ld[imgCss];
}
}

protected override void OnInit(EventArgs e)

{
base.OnInit(e);
}

protected override void Render(HtmlTextWriter writer)

{
string url = string.Format(@"{0}?{1}={2}&{3}={4}", this.ImgBuildFileUrl, Common.Web.UrlInfo.Key_Ctrl_Identity, Common.Web.UrlInfo.Value_WebCheckCode_Identity, Common.Web.UrlInfo.Key_WebCheckCode_Param, this.CheckWordNew);
writer.AddAttribute("src", url);
if (this._ImgCss != null && this._ImgCss != "")

{
writer.AddAttribute("class", this._ImgCss);
}
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
base.Render(writer);
}
#endregion
#endregion


Designer#region Designer

/**//// <summary>
/// <see cref="WebCheckCode"/> 服务器控件设计器。
/// </summary>
public class Designer : System.Web.UI.Design.ControlDesigner

{

/**//// <summary>
/// 获取用于在设计时表示关联控件的 HTML。
/// </summary>
/// <returns>用于在设计时表示控件的 HTML。</returns>
public override string GetDesignTimeHtml()

{
return "<span style='border-right: #ff6666 1px dotted; border-top: #ff6666 1px dotted; border-left: #ff6666 1px dotted; width: 20px; border-bottom: #ff6666 1px dotted; height: 70px; background-color: #ffcc66;font-size:13px; padding-right: 3px; padding-left: 3px; padding-bottom: 3px; padding-top: 3px;'>验证码</span>";
}
}
#endregion


WebCheckCode ashx#region WebCheckCode ashx
internal class Ashx_WebCheckCode : ICtrl_Ashx

{

/**//// <summary>
/// 随机数产生器
/// </summary>
private Random random = new Random();


ICtrl_Ashx Members#region ICtrl_Ashx Members

public void WriteContent(HttpResponse response, HttpRequest request)

{
response.Cache.SetCacheability(HttpCacheability.Public);
response.BufferOutput = false;
//写入图片
this.WriteSingleImage(this.ImgStr, response);
}


/**//// <summary>
/// 输出类型
/// </summary>
public string ContentType

{
get

{
return "image/gif";
}
}

#endregion

/**//// <summary>
/// 写入文字
/// </summary>
protected virtual string ImgStr

{
get

{
return Common.Web.UrlInfo.Value_WebCheckCode_Param;
}
}


/**//// <summary>
/// 图片类型
/// </summary>
protected virtual ImageFormat WriteImageFormat

{
get

{
return ImageFormat.Gif;
}
}


/**//// <summary>
/// 字体(GraphicsUnit 必须为像素Pixel)
/// </summary>
protected virtual Font WriteFont

{
get

{
string fontfamily = this.GetFontfamily();
FontStyle fontStyle = this.GetFontStyle();
float fontSize = this.GetFontSize();
return new System.Drawing.Font(fontfamily, fontSize, fontStyle, GraphicsUnit.Pixel);
}
}


/**//// <summary>
/// 得到随机字体大小
/// </summary>
/// <param name="r">随机类</param>
/// <returns>字体大小</returns>
protected float GetFontSize()

{
int i = this.random.Next(4);
if (i == 1)

{
return (float)16.3;
}
else if (i == 2)

{
return (float)16.4;
}
else if (i == 3)

{
return (float)16.5;
}
else

{
return (float)16.6;
}
}


/**//// <summary>
/// 得到随机字体样式
/// </summary>
/// <param name="r">随机类</param>
/// <returns>字体样式</returns>
protected FontStyle GetFontStyle()

{
int i = this.random.Next(4);
if (i == 1)

{
return FontStyle.Bold;
}
else if (i == 2)

{
return FontStyle.Underline;
}
else if (i == 3)

{
return FontStyle.Italic;
}
else

{
return FontStyle.Regular;
}
}


/**//// <summary>
/// 得到随机字体
/// </summary>
/// <returns>字体名</returns>
protected string GetFontfamily()

{
int i = this.random.Next(4);
if (i == 1)

{
return "Arial";
}
else if (i == 2)

{
return "Perpetua";
}
else if (i == 3)

{
return "Sylfaen";
}
else

{
return "Tw Cen MT";
}
}


/**//// <summary>
/// 文字颜色
/// </summary>
protected virtual Color WriteColor

{
get

{
int i = this.random.Next(4);
if (i == 1)

{
return Color.Red;
}
else if(i == 2)

{
return Color.Blue;
}
else if (i == 3)

{
return Color.SeaGreen;
}
else

{
return Color.Green;
}

}
}


/**//// <summary>
/// 文字宽度Pixel
/// </summary>
/// <param name="text">文字</param>
/// <param name="font">字体</param>
/// <returns>文字宽度Pixel</returns>
protected virtual int GetImgWidth(string text, Font font)

{
int i = Common.Img.TextUse.GetTextWidth(text, font, System.Text.ASCIIEncoding.ASCII);
if (text.Length == 1)
return i;
else if (text.Length > 1 && text.Length < 7)

{
return (int)(i * 4 / 5);
}
else

{
return (int)(i * 5 / 7);
}
}


/**//// <summary>
/// 文字高度Pixel
/// </summary>
/// <param name="font">字体</param>
/// <returns>文字高度Pixel</returns>
protected virtual int GetImgHeight(Font font)

{
return Common.Img.TextUse.GetTextHeight(font) + 4;
}


/**//// <summary>
/// 得到绘图
/// </summary>
/// <param name="b">Bitmap</param>
/// <returns></returns>
protected virtual Graphics GetImgGraphics(Bitmap b)

{
return Graphics.FromImage(b);
}


/**//// <summary>
/// 画背景
/// </summary>
/// <param name="g"></param>
/// <param name="width"></param>
/// <param name="height"></param>
protected virtual void DrawBackground(Graphics g, int width, int height)

{
Common.Img.DrawPic.DrawRoundRectangle(new SolidBrush(BackgroundColor), 0, 0, width, height, 0, g);
}


/**//// <summary>
/// 背景颜色
/// </summary>
protected virtual Color BackgroundColor

{
get

{
return Color.Wheat;
}
}


/**//// <summary>
/// 写入图片
/// </summary>
/// <param name="imgStr"></param>
protected virtual void WriteSingleImage(string imgStr, HttpResponse response)

{
int width = this.GetImgWidth(imgStr, this.WriteFont) + 4;
int height = this.GetImgHeight(this.WriteFont) + 1;
Bitmap b = new Bitmap(width, height);

Graphics g = this.GetImgGraphics(b);
this.DrawBackground(g, width, height);
this.WriteNoise(g, b);

Random r = new Random();
float oneWidth = width / imgStr.Length;
float x = (float)((r.Next(10) + 0.1) / 10);
for (int i = 0; i < imgStr.Length; i++)

{
SolidBrush brush = new SolidBrush(this.WriteColor);
float y = (float)((r.Next(10) + 0.1)) / 10;
g.DrawString(imgStr[i].ToString(), this.WriteFont, brush, x, y);
x += oneWidth;
}

System.IO.MemoryStream m = new System.IO.MemoryStream();
b.Save(m, this.WriteImageFormat);
byte[] bt = m.GetBuffer();
response.OutputStream.Write(bt, 0, bt.Length);
}


/**//// <summary>
/// 给图片加干扰
/// </summary>
/// <param name="graphics">Graphics</param>
/// <param name="bitmap">Bitmap</param>
protected virtual void WriteNoise(Graphics graphics, Bitmap bitmap)

{
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 25; i++)

{
int x1 = random.Next(bitmap.Width);
int x2 = random.Next(bitmap.Width);
int y1 = random.Next(bitmap.Height);
int y2 = random.Next(bitmap.Height);

graphics.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}

//画图片的前景噪音点
for (int i = 0; i < 100; i++)

{
int xx = random.Next(bitmap.Width);
int yy = random.Next(bitmap.Height);
bitmap.SetPixel(xx, yy, Color.FromArgb(random.Next()));
}

graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
}
}
#endregion
}

}
