验证码控件的改进

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

但还没解决设计时支持的问题。不知为何?

代码如下:
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Text;
None.gif
using System.Web;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Drawing;
None.gif
using System.Drawing.Imaging;
None.gif
None.gif
namespace Common.Web.AppControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 效验码控件
InBlock.gif    
/// </summary>
InBlock.gif    
/// <summary>
InBlock.gif    
/// 上传进度条控件
InBlock.gif    
/// </summary>
InBlock.gif    
/// <example>
InBlock.gif    
/// Web.config 配置
InBlock.gif    
/// <?xml version="1.0"?>
InBlock.gif    
///<configuration>
InBlock.gif    
///    <configSections>
InBlock.gif    
///        <sectionGroup name="dataGroup">
InBlock.gif    
///             <!--加密算法-->
InBlock.gif    
///             <section name="encrypt" type="Common.Framework.ProviderConfigurationHandler, Common"/>
InBlock.gif    
///        </sectionGroup>
InBlock.gif    
///    </configSections>
InBlock.gif    
///    <dataGroup>
InBlock.gif    
///            <encrypt defaultProvider="EncryptRe">
InBlock.gif    
///                <providers>
InBlock.gif    
///                  <clear/>
InBlock.gif    
///                  <!--可逆-->
InBlock.gif    
///                     <add name="EncryptRe" type="Common.DEncrypt.EncryptProvider, Common" classname="Common.DEncrypt.EncryptRe, Common"/>
InBlock.gif    
///                     <!--不可逆-->
InBlock.gif    
///                     <add name="EncryptUnRe" type="Common.DEncrypt.EncryptProvider, Common" classname="Common.DEncrypt.EncryptUnRe, Common"/>
InBlock.gif    
///                </providers>
InBlock.gif    
///            </encrypt>
InBlock.gif    
///   </dataGroup>
InBlock.gif    
///   <system.web>
InBlock.gif    
///         <httpHandlers>
InBlock.gif    
///           <add verb="*" path="CommonToolsASHX.ashx" type="Common.Web.AppControl.CommonToolsASHX, Common" />
InBlock.gif    
///         </httpHandlers>
InBlock.gif    
///   </system.web>
InBlock.gif    
///</configuration>
ExpandedSubBlockEnd.gif    
/// </example>

InBlock.gif    [Designer(typeof(WebCheckCode.Designer))]
InBlock.gif    [ToolboxData(
"<{0}:WebCheckCode runat=server></{0}:WebCheckCode>")]   
InBlock.gif    
public class WebCheckCode : Control, INamingContainer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
WebCheckCode#region WebCheckCode
ContractedSubBlock.gifExpandedSubBlockStart.gif        
private#region private
InBlock.gif        
private string checkwordNow
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "CheckWordNow" + this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string checkwordLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "CheckWordLength" + this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string checkwordType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "CheckWordType" + this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string imgBuildFileUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "ImgBuildFileUrl" + this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string imgCss
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "imgCss" + this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string GetRandomWord()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Common.StrHelp.Random random 
= new Common.StrHelp.Random(this.Length);
InBlock.gif            
return random.GetRandomWord(this.WordType);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _CheckWordNew = null;
InBlock.gif        
private string CheckWordNew
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this._CheckWordNew == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this._CheckWordNew = Common.DEncrypt.Factory.GetInstanceRe().EncryptString(this.GetRandomWord());
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return this._CheckWordNew;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _CheckWordNow;
InBlock.gif        
protected string _ImgBuildFileUrl = Common.Web.UrlInfo.Url_CommonToolsASHX;
InBlock.gif        
private string _ImgCss = null;
InBlock.gif        
private Common.StrHelp.RandomWordType _WordType = Common.StrHelp.RandomWordType.Num;
InBlock.gif        
private int _CheckWorkLength = 4;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Value 效验码值#region Value 效验码值
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 效验码值
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public string Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Common.DEncrypt.Factory.GetInstanceRe().DecryptString(this._CheckWordNow);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
WordType 控件字符串类型#region WordType 控件字符串类型
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 控件字符串类型 WordType
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public Common.StrHelp.RandomWordType WordType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._WordType;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._WordType = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Length 效验码长度#region Length 效验码长度
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 效验码长度
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"4")]
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public int Length
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._CheckWorkLength;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._CheckWorkLength = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ImgBuildFileUrl 输出图片的地址#region ImgBuildFileUrl 输出图片的地址
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输出图片的地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [Localizable(
true)]
InBlock.gif        [UrlProperty]
InBlock.gif        
public virtual string ImgBuildFileUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._ImgBuildFileUrl;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._ImgBuildFileUrl = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ImgCss 图片样式#region ImgCss 图片样式
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 图片样式
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public string ImgCss
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._ImgCss;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._ImgCss = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
override#region override
InBlock.gif        
protected override object SaveViewState()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Collections.Specialized.ListDictionary ld 
= new System.Collections.Specialized.ListDictionary();
InBlock.gif            ld.Add(checkwordType, 
this._WordType);
InBlock.gif            ld.Add(checkwordLength, 
this._CheckWorkLength);
InBlock.gif            ld.Add(checkwordNow, 
this.CheckWordNew);
InBlock.gif            ld.Add(imgBuildFileUrl, 
this._ImgBuildFileUrl);
InBlock.gif            ld.Add(imgCss, 
this._ImgCss);
InBlock.gif            
return ld;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void LoadViewState(object savedState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (savedState != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Collections.Specialized.ListDictionary ld 
= (System.Collections.Specialized.ListDictionary)savedState;
InBlock.gif                
this._WordType = (Common.StrHelp.RandomWordType)ld[checkwordType];
InBlock.gif                
this._CheckWorkLength = (int)ld[checkwordLength];
InBlock.gif                
this._CheckWordNow = (string)ld[checkwordNow];
InBlock.gif                
this._ImgBuildFileUrl = (string)ld[imgBuildFileUrl];
InBlock.gif                
this._ImgCss = (string)ld[imgCss];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
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);
InBlock.gif            writer.AddAttribute(
"src", url);
InBlock.gif            
if (this._ImgCss != null && this._ImgCss != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.AddAttribute(
"class"this._ImgCss);
ExpandedSubBlockEnd.gif            }

InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Img);
InBlock.gif            writer.RenderEndTag();
InBlock.gif            
base.Render(writer);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Designer#region Designer
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// <see cref="WebCheckCode"/> 服务器控件设计器。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public class Designer : System.Web.UI.Design.ControlDesigner
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 获取用于在设计时表示关联控件的 HTML。
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <returns>用于在设计时表示控件的 HTML。</returns>

InBlock.gif            public override string GetDesignTimeHtml()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
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>";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
WebCheckCode ashx#region WebCheckCode ashx
InBlock.gif        
internal class Ashx_WebCheckCode : ICtrl_Ashx
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 随机数产生器
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            private Random random = new Random();
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif            
ICtrl_Ashx Members#region ICtrl_Ashx Members
InBlock.gif
InBlock.gif            
public void WriteContent(HttpResponse response, HttpRequest request)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                response.Cache.SetCacheability(HttpCacheability.Public);
InBlock.gif                response.BufferOutput 
= false;
InBlock.gif                
//写入图片
InBlock.gif
                this.WriteSingleImage(this.ImgStr, response);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 输出类型
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public string ContentType
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "image/gif";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif            
#endregion

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 写入文字
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            protected virtual string ImgStr
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return Common.Web.UrlInfo.Value_WebCheckCode_Param;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif           
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 图片类型
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            protected virtual ImageFormat WriteImageFormat
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ImageFormat.Gif;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 字体(GraphicsUnit 必须为像素Pixel)
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            protected virtual Font WriteFont
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string fontfamily = this.GetFontfamily();
InBlock.gif                    FontStyle fontStyle 
= this.GetFontStyle();
InBlock.gif                    
float fontSize = this.GetFontSize();
InBlock.gif                   
InBlock.gif                    
return new System.Drawing.Font(fontfamily, fontSize, fontStyle, GraphicsUnit.Pixel);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 得到随机字体大小
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="r">随机类</param>
ExpandedSubBlockEnd.gif            
/// <returns>字体大小</returns>

InBlock.gif            protected float GetFontSize()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i = this.random.Next(4);
InBlock.gif                
if (i == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return (float)16.3;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (i == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return (float)16.4;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (i == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return (float)16.5;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return (float)16.6;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 得到随机字体样式
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="r">随机类</param>
ExpandedSubBlockEnd.gif            
/// <returns>字体样式</returns>

InBlock.gif            protected FontStyle GetFontStyle()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i = this.random.Next(4);
InBlock.gif                
if (i == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return FontStyle.Bold;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (i == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return FontStyle.Underline;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (i == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return FontStyle.Italic;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return FontStyle.Regular;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 得到随机字体
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <returns>字体名</returns>

InBlock.gif            protected string GetFontfamily()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i = this.random.Next(4);
InBlock.gif                
if (i == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "Arial";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (i == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "Perpetua";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (i == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "Sylfaen";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "Tw Cen MT";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 文字颜色
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            protected virtual Color WriteColor
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int i = this.random.Next(4);
InBlock.gif                    
if (i == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return Color.Red;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if(i == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return Color.Blue;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (i == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return Color.SeaGreen;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return Color.Green;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 文字宽度Pixel
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="text">文字</param>
InBlock.gif            
/// <param name="font">字体</param>
ExpandedSubBlockEnd.gif            
/// <returns>文字宽度Pixel</returns>

InBlock.gif            protected virtual int GetImgWidth(string text, Font font)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i = Common.Img.TextUse.GetTextWidth(text, font, System.Text.ASCIIEncoding.ASCII);
InBlock.gif                
if (text.Length == 1)
InBlock.gif                    
return i;
InBlock.gif                
else if (text.Length > 1 && text.Length < 7)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return (int)(i * 4 / 5);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return (int)(i * 5 / 7);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 文字高度Pixel
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="font">字体</param>
ExpandedSubBlockEnd.gif            
/// <returns>文字高度Pixel</returns>

InBlock.gif            protected virtual int GetImgHeight(Font font)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Common.Img.TextUse.GetTextHeight(font) + 4;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 得到绘图
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="b">Bitmap</param>
ExpandedSubBlockEnd.gif            
/// <returns></returns>

InBlock.gif            protected virtual Graphics GetImgGraphics(Bitmap b)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Graphics.FromImage(b);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 画背景
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="g"></param>
InBlock.gif            
/// <param name="width"></param>
ExpandedSubBlockEnd.gif            
/// <param name="height"></param>

InBlock.gif            protected virtual void DrawBackground(Graphics g, int width, int height)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Common.Img.DrawPic.DrawRoundRectangle(
new SolidBrush(BackgroundColor), 00, width, height, 0, g);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 背景颜色
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            protected virtual Color BackgroundColor
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return Color.Wheat;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 写入图片
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="imgStr"></param>

InBlock.gif            protected virtual void WriteSingleImage(string imgStr, HttpResponse response)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int width = this.GetImgWidth(imgStr, this.WriteFont) + 4;
InBlock.gif                
int height = this.GetImgHeight(this.WriteFont) + 1;
InBlock.gif                Bitmap b 
= new Bitmap(width, height);
InBlock.gif
InBlock.gif                Graphics g 
= this.GetImgGraphics(b);
InBlock.gif                
this.DrawBackground(g, width, height);
InBlock.gif               
InBlock.gif                
this.WriteNoise(g, b);
InBlock.gif
InBlock.gif                Random r 
= new Random();
InBlock.gif                
float oneWidth = width / imgStr.Length;
InBlock.gif                
float x = (float)((r.Next(10+ 0.1/ 10);
InBlock.gif               
InBlock.gif                
for (int i = 0; i < imgStr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    SolidBrush brush 
= new SolidBrush(this.WriteColor);
InBlock.gif                    
float y = (float)((r.Next(10+ 0.1)) / 10;
InBlock.gif                    g.DrawString(imgStr[i].ToString(), 
this.WriteFont, brush, x, y);
InBlock.gif                    x 
+= oneWidth;
ExpandedSubBlockEnd.gif                }

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 给图片加干扰
InBlock.gif            
/// </summary>
InBlock.gif            
/// <param name="graphics">Graphics</param>
ExpandedSubBlockEnd.gif            
/// <param name="bitmap">Bitmap</param>

InBlock.gif            protected virtual void WriteNoise(Graphics graphics, Bitmap bitmap)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Random random 
= new Random();
InBlock.gif                
//画图片的背景噪音线
InBlock.gif
                for (int i = 0; i < 25; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int x1 = random.Next(bitmap.Width);
InBlock.gif                    
int x2 = random.Next(bitmap.Width);
InBlock.gif                    
int y1 = random.Next(bitmap.Height);
InBlock.gif                    
int y2 = random.Next(bitmap.Height);
InBlock.gif
InBlock.gif                    graphics.DrawLine(
new Pen(Color.Silver), x1, y1, x2, y2);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//画图片的前景噪音点
InBlock.gif
                for (int i = 0; i < 100; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int xx = random.Next(bitmap.Width);
InBlock.gif                    
int yy = random.Next(bitmap.Height);
InBlock.gif                    bitmap.SetPixel(xx, yy, Color.FromArgb(random.Next()));
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                graphics.DrawRectangle(
new Pen(Color.Silver), 00, bitmap.Width - 1, bitmap.Height - 1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif


转载于:https://www.cnblogs.com/laiwen/archive/2006/12/05/582613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值