注册时的图片验证码

注册时的图⽚验证码
⽣成图⽚验证码
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.HtmlControls;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Xml.Linq;
11using System.Drawing;
12using System.Text;
13
14///<summary>
15/// VdCode 的摘要说明
16///</summary>
17public class VdCode
管式直线电机
18 {
19///<summary>
20///全局随机数⽣成器
21///</summary>
22private Random rndNumber;
23public static string mrChineseChars = String.Empty;
24///<summary>
25///英⽂与数字串
26///</summary>
27// protected static readonly string mrEnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 28protected static readonly string mrEnglishOrNumChars = "0123456789";
29
30public VdCode()
31    {
32        rndNumber = new Random(unchecked((int)DateTime.Now.Ticks));
33    }
34// 验证码长度(默认6个验证码的长度)
35int length = 5;
36public int Length
37    {
38get { return length; }
39set { length = value; }
40    }
41//验证码字体⼤⼩
42// int fontSize = 18;
43int fontSize = 18;
44public int FontSize
45    {
46get { return fontSize; }
47set { fontSize = value; }
48    }
49// 边框补(默认4像素)
50//int padding = 4;
51int padding = 4;
52public int Padding
53    {
54get { return padding; }
55set { padding = value; }
56    }
57// 是否输出燥点(默认输出)
58bool chaos = true;
59public bool Chaos
60    {
61get { return chaos; }
62set { chaos = value; }
63    }
64// 输出燥点的颜⾊(默认灰⾊)
65    Color chaosColor = Color.LightGray;
66public Color ChaosColor
67    {
68get { return chaosColor; }
69set { chaosColor = value; }
70    }
71// ⾃定义背景⾊(默认⽩⾊)
72    Color backgroundColor = Color.White;
73public Color BackgroundColor
74    {
75get { return backgroundColor; }
76set { backgroundColor = value; }
77    }
78// ⾃定义随机颜⾊数组
79    Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 80public Color[] Colors
81    {
82get { return colors; }
83set { colors = value; }
84    }
85// ⾃定义字体数组
86string[] fonts = { "Arial", "Georgia" };
87public string[] Fonts
88    {
89get { return fonts; }
计费系统
90set { fonts = value; }
91    }
92#region产⽣扭曲图⽚
93private const double PI = 3.1415926535897932384626433832795;
94private const double PI2 = 6.283185307179586476925286766559;
95//private const double PI =0;
96//private const double PI2 =0;
97///<summary>
98///该⽅法⽤于扭曲图⽚
99///</summary>
100///<param name="srcBmp">图⽚路径</param>
101///<param name="bXDir">如果扭曲则选择为True</param>
102///<param name="nMultValue">波形的幅度倍数,越⼤扭曲的程度越⾼,⼀般为3</param>
103///<param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
104///<returns></returns>
105public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
106    {
107        System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);//创建Bitmap对象
108// 将位图背景填充为⽩⾊固化闪电之源
109        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(destBmp);//创建Graphics对象
110        g.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);//将位图背景填充为⽩⾊111        g.Dispose();//释放raphics对象
112double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;//判断扭曲⽅式
113for (int i = 0; i < destBmp.Width; i++)
114        {
115for (int j = 0; j < destBmp.Height; j++)
116            {
117double dx = 0;
118                dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
119                dx += dPhase;
120double dy = Math.Sin(dx);
121// 取得当前点的颜⾊
122int nOldX = 0, nOldY = 0;
123                nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
124                nOldY = bXDir ? j : j + (int)(dy * dMultValue);
125                System.Drawing.Color color = srcBmp.GetPixel(i, j);
126if (nOldX >= 0 && nOldX < destBmp.Width
127                  && nOldY >= 0 && nOldY < destBmp.Height)
128                {
129                    destBmp.SetPixel(nOldX, nOldY, color);
130                }
131            }
132        }
133return destBmp;
134    }
135#endregion
136///<summary>
137///⽣成校验码图⽚
138///</summary>
139///<param name="code">验证码</param>
140///<returns></returns>
141public Bitmap CreateImage(string code)
142    {
143int fSize = FontSize;
144int fWidth = fSize + Padding;
145int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
146int imageHeight = fSize * 2 + Padding * 2;
147        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
148        Graphics g = Graphics.FromImage(image);
149        g.Clear(BackgroundColor);
150//给背景添加随机⽣成的燥点
151if (this.Chaos)
152        {
153            Pen pen = new Pen(ChaosColor, 0);
154int c = Length * 10;
155for (int i = 0; i < c; i++)
156            {
157int x = rndNumber.Next(image.Width);
158int y = rndNumber.Next(image.Height);
159
160                g.DrawRectangle(pen, x, y, 1, 1);
161            }
162        }
163int left = 0, top = 0, top1 = 1, top2 = 1;
164int n1 = (imageHeight - FontSize - Padding * 2);
165int n2 = n1 / 4;
166        top1 = n2;
167        top2 = n2 * 2;
168        Font f;
169        Brush b;
170int cindex, findex;
171//随机字体和颜⾊的验证码字符
172for (int i = 0; i < code.Length; i++)
173        {
174            cindex = rndNumber.Next(Colors.Length - 1);
175            findex = rndNumber.Next(Fonts.Length - 1);
176            f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold); 177            b = new System.Drawing.SolidBrush(Colors[cindex]);
178if (i % 2 == 1)
179            {
180                top = top2;
181            }
182else
183            {
184                top = top1;
185            }
186            left = i * fWidth;
187            g.DrawString(code.Substring(i, 1), f, b, left, top);
188        }
189//画⼀个边框边框颜⾊为Color.Gainsboro
190        g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1); 191        g.Dispose();
192//产⽣波形(Add By 51aspx)
193//image = TwistImage(image, true, 8, 4);
194        image = TwistImage(image, false, 0, 0);
195return image;
196    }
197///<summary>
198///⽣成随机字符码
199///</summary>
200///<param name="codeLen">字符串长度</param>
201///<param name="zhCharsCount">中⽂字符数</param>
202///<returns></returns>
203public string CreateVerifyCode(int codeLen, int zhCharsCount)
204    {
205char[] chs = new char[codeLen];
206
207int index;
208for (int i = 0; i < zhCharsCount; i++)
209        {
210            index = rndNumber.Next(0, codeLen);
211if (chs[index] == '\0')
212                chs[index] = CreateZhChar();
213else
214                --i;
215        }
216for (int i = 0; i < codeLen; i++)
217        {
218if (chs[i] == '\0')
219                chs[i] = CreateEnOrNumChar();
220        }
221
222return new string(chs, 0, chs.Length);
223    }
224// ⽣成默认长度5的随机字符码
225public string CreateVerifyCode()
226    {
227return CreateVerifyCode(Length, 0);
228    }
229// ⽣成英⽂或数字字符
230protected char CreateEnOrNumChar()
231    {
232return mrEnglishOrNumChars[rndNumber.Next(0, mrEnglishOrNumChars.Length)];
233    }
234protected char CreateZhChar() // ⽣成汉字字符
235    {
236//若提供了汉字集,查询汉字集选取汉字
237if (mrChineseChars.Length > 0)
238        {
239return mrChineseChars[rndNumber.Next(0, mrChineseChars.Length)];
240        }
241//若没有提供汉字集,则根据《GB2312简体中⽂编码表》编码规则构造汉字
242else
243        {
244byte[] bytes = new byte[2];
246//第⼀个字节值在0xb0, 0xf7之间
247            bytes[0] = (byte)rndNumber.Next(0xb0, 0xf8);
248//第⼆个字节值在0xa1, 0xfe之间
249            bytes[1] = (byte)rndNumber.Next(0xa1, 0xff);
250
251//根据汉字编码的字节数组解码出中⽂汉字
252string str1 = Encoding.GetEncoding("gb2312").GetString(bytes);
253
254return str1[0];
255        }
256    }
257 }
View Code
调⽤56短信王的短信接⼝
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.HtmlControls;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Xml.Linq;
11using System.Drawing;
12using System.Text;
13
14///<summary>
15/// VdCode 的摘要说明
16///</summary>
17public class VdCode
18 {
19///<summary>
20///全局随机数⽣成器
21///</summary>
22private Random rndNumber;
23public static string mrChineseChars = String.Empty;
24///<summary>
25///英⽂与数字串
26///</summary>
27// protected static readonly string mrEnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 28protected static readonly string mrEnglishOrNumChars = "0123456789";
29
30public VdCode()
31    {
32        rndNumber = new Random(unchecked((int)DateTime.Now.Ticks));
免蒸加气砖33    }
34// 验证码长度(默认6个验证码的长度)
35int length = 5;
36public int Length
37    {
38get { return length; }
39set { length = value; }
40    }
41//验证码字体⼤⼩
42// int fontSize = 18;
43int fontSize = 18;
44public int FontSize
45    {
46get { return fontSize; }
47set { fontSize = value; }
48    }
49// 边框补(默认4像素)
50//int padding = 4;
51int padding = 4;
52public int Padding
53    {
54get { return padding; }
55set { padding = value; }
56    }
非接触式扭矩传感器
57// 是否输出燥点(默认输出)
58bool chaos = true;
59public bool Chaos
60    {
61get { return chaos; }
62set { chaos = value; }
63    }
溶洞处理
64// 输出燥点的颜⾊(默认灰⾊)
65    Color chaosColor = Color.LightGray;
66public Color ChaosColor
68get { return chaosColor; }
69set { chaosColor = value; }
70    }
71// ⾃定义背景⾊(默认⽩⾊)
72    Color backgroundColor = Color.White;
73public Color BackgroundColor
74    {
75get { return backgroundColor; }
76set { backgroundColor = value; }
77    }
78// ⾃定义随机颜⾊数组
79    Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 80public Color[] Colors
81    {
82get { return colors; }
83set { colors = value; }
84    }
85// ⾃定义字体数组
86string[] fonts = { "Arial", "Georgia" };
87public string[] Fonts
88    {
89get { return fonts; }
90set { fonts = value; }
91    }
92#region产⽣扭曲图⽚
93private const double PI = 3.1415926535897932384626433832795;
94private const double PI2 = 6.283185307179586476925286766559;
95//private const double PI =0;
96//private const double PI2 =0;
97///<summary>
98///该⽅法⽤于扭曲图⽚
99///</summary>
100///<param name="srcBmp">图⽚路径</param>
101///<param name="bXDir">如果扭曲则选择为True</param>
102///<param name="nMultValue">波形的幅度倍数,越⼤扭曲的程度越⾼,⼀般为3</param>
103///<param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
104///<returns></returns>
105public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
106    {
107        System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);//创建Bitmap对象
108// 将位图背景填充为⽩⾊
109        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(destBmp);//创建Graphics对象
110        g.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);//将位图背景填充为⽩⾊111        g.Dispose();//释放raphics对象
112double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;//判断扭曲⽅式
113for (int i = 0; i < destBmp.Width; i++)
114        {
115for (int j = 0; j < destBmp.Height; j++)
116            {
117double dx = 0;
118                dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
119                dx += dPhase;
120double dy = Math.Sin(dx);
121// 取得当前点的颜⾊
122int nOldX = 0, nOldY = 0;
123                nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
124                nOldY = bXDir ? j : j + (int)(dy * dMultValue);
125                System.Drawing.Color color = srcBmp.GetPixel(i, j);
126if (nOldX >= 0 && nOldX < destBmp.Width
127                  && nOldY >= 0 && nOldY < destBmp.Height)
128                {
129                    destBmp.SetPixel(nOldX, nOldY, color);
130                }
131            }
132        }
133return destBmp;
134    }
135#endregion
136///<summary>
137///⽣成校验码图⽚
138///</summary>
139///<param name="code">验证码</param>
140///<returns></returns>
141public Bitmap CreateImage(string code)
142    {
143int fSize = FontSize;
144int fWidth = fSize + Padding;
145int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
146int imageHeight = fSize * 2 + Padding * 2;
147        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
148        Graphics g = Graphics.FromImage(image);
149        g.Clear(BackgroundColor);
150//给背景添加随机⽣成的燥点

本文发布于:2024-09-23 03:16:53,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/219070.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:默认   验证码   扭曲   背景   数组
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议