中文彩色驗證碼實現(變形/噪點/點選更換

assureo發表於2008-01-13
                 1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.Drawing;
 11using System.Text;
 12
 13/**//// <summary>
 14/// VryImgGen 的摘要說明
 15/// </summary>
 16public class VryImgGen
 17...{
 18    public static string ChineseChars = String.Empty;
 19
 20    /**//// <summary>
 21    /// 英文與數字串
 22    /// </summary>
 23    protected static readonly string EnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 24
 25    public VryImgGen()
 26    ...{
 27        rnd = new Random(unchecked((int)DateTime.Now.Ticks));
 28    }
 29//該原始碼下載自www.51aspx.com(51aspx.com)
 30
 31    /**//// <summary>
 32    /// 全域性隨機數生成器
 33    /// </summary>
 34    private Random rnd;
 35
 36    int length = 5;
 37    /**//// <summary>
 38    /// 驗證碼長度(預設6個驗證碼的長度)
 39    /// </summary>
 40    public int Length
 41    ...{
 42        get ...{ return length; }
 43        set ...{ length = value; }
 44    }
 45
 46    int fontSize = 18;
 47    /**//// <summary>
 48    /// 驗證碼字型大小(為了顯示扭曲效果,預設30畫素,可以自行修改)
 49    /// </summary>
 50    public int FontSize
 51    ...{
 52        get ...{ return fontSize; }
 53        set ...{ fontSize = value; }
 54    }
 55
 56    int padding = 4;
 57    /**//// <summary>
 58    /// 邊框補(預設4畫素)
 59    /// </summary>
 60    public int Padding
 61    ...{
 62        get ...{ return padding; }
 63        set ...{ padding = value; }
 64    }
 65
 66    bool chaos = true;
 67    /**//// <summary>
 68    /// 是否輸出燥點(預設輸出)
 69    /// </summary>
 70    public bool Chaos
 71    ...{
 72        get ...{ return chaos; }
 73        set ...{ chaos = value; }
 74    }
 75
 76    Color chaosColor = Color.LightGray;
 77    /**//// <summary>
 78    /// 輸出燥點的顏色(預設灰色)
 79    /// </summary>
 80    public Color ChaosColor
 81    ...{
 82        get ...{ return chaosColor; }
 83        set ...{ chaosColor = value; }
 84    }
 85
 86    Color backgroundColor = Color.White;
 87    /**//// <summary>
 88    /// 自定義背景色(預設白色)
 89    /// </summary>
 90    public Color BackgroundColor
 91    ...{
 92        get ...{ return backgroundColor; }
 93        set ...{ backgroundColor = value; }
 94    }
 95
 96    Color[] colors = ...{ Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
 97    /**//// <summary>
 98    /// 自定義隨機顏色陣列
 99    /// </summary>
100    public Color[] Colors
101    ...{
102        get ...{ return colors; }
103        set ...{ colors = value; }
104    }
105
106    string[] fonts = ...{ "Arial", "Georgia" };
107    /**//// <summary>
108    /// 自定義字型陣列
109    /// </summary>
110    public string[] Fonts
111    ...{
112        get ...{ return fonts; }
113        set ...{ fonts = value; }
114    }
115
116    產生波形濾鏡效果#region 產生波形濾鏡效果
117
118    private const double PI = 3.1415926535897932384626433832795;
119    private const double PI2 = 6.283185307179586476925286766559;
120
121    /**//// <summary>
122    /// 正弦曲線Wave扭曲圖片(Edit By 51aspx.com)
123    /// </summary>
124    /// <param name="srcBmp">圖片路徑</param>
125    /// <param name="bXDir">如果扭曲則選擇為True</param>
126    /// <param name="nMultValue">波形的幅度倍數,越大扭曲的程度越高,一般為3</param>
127    /// <param name="dPhase">波形的起始相位,取值區間[0-2*PI)</param>
128    /// <returns></returns>
129    public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
130    ...{
131        System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
132
133        // 將點陣圖背景填充為白色
134        System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
135        graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
136        graph.Dispose();
137
138        double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
139
140        for (int i = 0; i < destBmp.Width; i++)
141        ...{
142            for (int j = 0; j < destBmp.Height; j++)
143            ...{
144                double dx = 0;
145                dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
146                dx += dPhase;
147                double dy = Math.Sin(dx);
148
149                // 取得當前點的顏色
150                int nOldX = 0, nOldY = 0;
151                nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
152                nOldY = bXDir ? j : j + (int)(dy * dMultValue);
153
154                System.Drawing.Color color = srcBmp.GetPixel(i, j);
155                if (nOldX >= 0 && nOldX < destBmp.Width
156                 && nOldY >= 0 && nOldY < destBmp.Height)
157                ...{
158                    destBmp.SetPixel(nOldX, nOldY, color);
159                }
160            }
161        }
162
163        return destBmp;
164    }
165
166
167
168    #endregion
169
170    /**//// <summary>
171    /// 生成校驗碼圖片
172    /// </summary>
173    /// <param name="code">驗證碼</param>
174    /// <returns></returns>
175    public Bitmap CreateImage(string code)
176    ...{
177        int fSize = FontSize;
178        int fWidth = fSize + Padding;
179
180        int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
181        int imageHeight = fSize * 2 + Padding * 2;
182
183        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
184
185        Graphics g = Graphics.FromImage(image);
186
187        g.Clear(BackgroundColor);
188
189        //給背景新增隨機生成的燥點
190        if (this.Chaos)
191        ...{
192
193            Pen pen = new Pen(ChaosColor, 0);
194            int c = Length * 10;
195
196            for (int i = 0; i < c; i++)
197            ...{
198                int x = rnd.Next(image.Width);
199                int y = rnd.Next(image.Height);
200
201                g.DrawRectangle(pen, x, y, 1, 1);
202            }
203        }
204
205        int left = 0, top = 0, top1 = 1, top2 = 1;
206
207        int n1 = (imageHeight - FontSize - Padding * 2);
208        int n2 = n1 / 4;
209        top1 = n2;
210        top2 = n2 * 2;
211
212        Font f;
213        Brush b;
214
215        int cindex, findex;
216
217        //隨機字型和顏色的驗證碼字元
218        for (int i = 0; i < code.Length; i++)
219        ...{
220            cindex = rnd.Next(Colors.Length - 1);
221            findex = rnd.Next(Fonts.Length - 1);
222
223            f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
224            b = new System.Drawing.SolidBrush(Colors[cindex]);
225
226            if (i % 2 == 1)
227            ...{
228                top = top2;
229            }
230            else
231            ...{
232                top = top1;
233            }
234
235            left = i * fWidth;
236
237            g.DrawString(code.Substring(i, 1), f, b, left, top);
238        }
239
240        //畫一個邊框 邊框顏色為Color.Gainsboro
241        g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
242        g.Dispose();
243
244        //產生波形(Add By 51aspx.com)
245        image = TwistImage(image, true, 8, 4);
246
247        return image;
248    }
249
250    /**//// <summary>
251    /// 生成隨機字元碼
252    /// </summary>
253    /// <param name="codeLen">字串長度</param>
254    /// <param name="zhCharsCount">中文字元數</param>
255    /// <returns></returns>
256    public string CreateVerifyCode(int codeLen, int zhCharsCount)
257    ...{
258        char[] chs = new char[codeLen];
259
260        int index;
261        for (int i = 0; i < zhCharsCount; i++)
262        ...{
263            index = rnd.Next(0, codeLen);
264            if (chs[index] == '/0')
265                chs[index] = CreateZhChar();
266            else
267                --i;
268        }
269        for (int i = 0; i < codeLen; i++)
270        ...{
271            if (chs[i] == '/0')
272                chs[i] = CreateEnOrNumChar();
273        }
274
275        return new string(chs, 0, chs.Length);
276    }
277
278    /**//// <summary>
279    /// 生成預設長度5的隨機字元碼
280    /// </summary>
281    /// <returns></returns>
282    public string CreateVerifyCode()
283    ...{
284        return CreateVerifyCode(Length, 0);
285    }
286
287    /**//// <summary>
288    /// 生成英文或數字字元
289    /// </summary>
290    /// <returns></returns>
291    protected char CreateEnOrNumChar()
292    ...{
293        return EnglishOrNumChars[rnd.Next(0, EnglishOrNumChars.Length)];
294    }
295
296    /**//// <summary>
297    /// 生成漢字字元
298    /// </summary>
299    /// <returns></returns>
300    protected char CreateZhChar()
301    ...{
302        //若提供了漢字集,查詢漢字集選取漢字
303        if (ChineseChars.Length > 0)
304        ...{
305            return ChineseChars[rnd.Next(0, ChineseChars.Length)];
306        }
307        //若沒有提供漢字集,則根據《GB2312簡體中文編碼表》編碼規則構造漢字
308        else
309        ...{
310            byte[] bytes = new byte[2];
311
312            //第一個位元組值在0xb0, 0xf7之間
313            bytes[0] = (byte)rnd.Next(0xb0, 0xf8);
314            //第二個位元組值在0xa1, 0xfe之間
315            bytes[1] = (byte)rnd.Next(0xa1, 0xff);
316
317            //根據漢字編碼的位元組陣列解碼出中文漢字
318            string str1 = Encoding.GetEncoding("gb2312").GetString(bytes);
319
320            return str1[0];
321        }
322    }
323
324}
325
   

相關文章