注册时的图片验证码
生成图片验证码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.HtmlControls; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Xml.Linq; 11 using System.Drawing; 12 using System.Text; 13 14 /// <summary> 15 /// VdCode 的摘要说明 16 /// </summary> 17 public class VdCode 18 { 19 /// <summary> 20 /// 全局随机数生成器 21 /// </summary> 22 private Random rndNumber; 23 public static string mrChineseChars = String.Empty; 24 /// <summary> 25 /// 英文与数字串 26 /// </summary> 27 // protected static readonly string mrEnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 28 protected static readonly string mrEnglishOrNumChars = "0123456789"; 29 30 public VdCode() 31 { 32 rndNumber = new Random(unchecked((int)DateTime.Now.Ticks)); 33 } 34 // 验证码长度(默认6个验证码的长度) 35 int length = 5; 36 public int Length 37 { 38 get { return length; } 39 set { length = value; } 40 } 41 //验证码字体大小 42 // int fontSize = 18; 43 int fontSize = 18; 44 public int FontSize 45 { 46 get { return fontSize; } 47 set { fontSize = value; } 48 } 49 // 边框补(默认4像素) 50 //int padding = 4; 51 int padding = 4; 52 public int Padding 53 { 54 get { return padding; } 55 set { padding = value; } 56 } 57 // 是否输出燥点(默认输出) 58 bool chaos = true; 59 public bool Chaos 60 { 61 get { return chaos; } 62 set { chaos = value; } 63 } 64 // 输出燥点的颜色(默认灰色) 65 Color chaosColor = Color.LightGray; 66 public Color ChaosColor 67 { 68 get { return chaosColor; } 69 set { chaosColor = value; } 70 } 71 // 自定义背景色(默认白色) 72 Color backgroundColor = Color.White; 73 public Color BackgroundColor 74 { 75 get { return backgroundColor; } 76 set { backgroundColor = value; } 77 } 78 // 自定义随机颜色数组 79 Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 80 public Color[] Colors 81 { 82 get { return colors; } 83 set { colors = value; } 84 } 85 // 自定义字体数组 86 string[] fonts = { "Arial", "Georgia" }; 87 public string[] Fonts 88 { 89 get { return fonts; } 90 set { fonts = value; } 91 } 92 #region 产生扭曲图片 93 private const double PI = 3.1415926535897932384626433832795; 94 private 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> 105 public 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对象 112 double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;//判断扭曲方式 113 for (int i = 0; i < destBmp.Width; i++) 114 { 115 for (int j = 0; j < destBmp.Height; j++) 116 { 117 double dx = 0; 118 dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen; 119 dx += dPhase; 120 double dy = Math.Sin(dx); 121 // 取得当前点的颜色 122 int 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); 126 if (nOldX >= 0 && nOldX < destBmp.Width 127 && nOldY >= 0 && nOldY < destBmp.Height) 128 { 129 destBmp.SetPixel(nOldX, nOldY, color); 130 } 131 } 132 } 133 return destBmp; 134 } 135 #endregion 136 /// <summary> 137 /// 生成校验码图片 138 /// </summary> 139 /// <param name="code">验证码</param> 140 /// <returns></returns> 141 public Bitmap CreateImage(string code) 142 { 143 int fSize = FontSize; 144 int fWidth = fSize + Padding; 145 int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2; 146 int 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 //给背景添加随机生成的燥点 151 if (this.Chaos) 152 { 153 Pen pen = new Pen(ChaosColor, 0); 154 int c = Length * 10; 155 for (int i = 0; i < c; i++) 156 { 157 int x = rndNumber.Next(image.Width); 158 int y = rndNumber.Next(image.Height); 159 160 g.DrawRectangle(pen, x, y, 1, 1); 161 } 162 } 163 int left = 0, top = 0, top1 = 1, top2 = 1; 164 int n1 = (imageHeight - FontSize - Padding * 2); 165 int n2 = n1 / 4; 166 top1 = n2; 167 top2 = n2 * 2; 168 Font f; 169 Brush b; 170 int cindex, findex; 171 //随机字体和颜色的验证码字符 172 for (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]); 178 if (i % 2 == 1) 179 { 180 top = top2; 181 } 182 else 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.com) 193 //image = TwistImage(image, true, 8, 4); 194 image = TwistImage(image, false, 0, 0); 195 return image; 196 } 197 /// <summary> 198 /// 生成随机字符码 199 /// </summary> 200 /// <param name="codeLen">字符串长度</param> 201 /// <param name="zhCharsCount">中文字符数</param> 202 /// <returns></returns> 203 public string CreateVerifyCode(int codeLen, int zhCharsCount) 204 { 205 char[] chs = new char[codeLen]; 206 207 int index; 208 for (int i = 0; i < zhCharsCount; i++) 209 { 210 index = rndNumber.Next(0, codeLen); 211 if (chs[index] == '\0') 212 chs[index] = CreateZhChar(); 213 else 214 --i; 215 } 216 for (int i = 0; i < codeLen; i++) 217 { 218 if (chs[i] == '\0') 219 chs[i] = CreateEnOrNumChar(); 220 } 221 222 return new string(chs, 0, chs.Length); 223 } 224 // 生成默认长度5的随机字符码 225 public string CreateVerifyCode() 226 { 227 return CreateVerifyCode(Length, 0); 228 } 229 // 生成英文或数字字符 230 protected char CreateEnOrNumChar() 231 { 232 return mrEnglishOrNumChars[rndNumber.Next(0, mrEnglishOrNumChars.Length)]; 233 } 234 protected char CreateZhChar() // 生成汉字字符 235 { 236 //若提供了汉字集,查询汉字集选取汉字 237 if (mrChineseChars.Length > 0) 238 { 239 return mrChineseChars[rndNumber.Next(0, mrChineseChars.Length)]; 240 } 241 //若没有提供汉字集,则根据《GB2312简体中文编码表》编码规则构造汉字 242 else 243 { 244 byte[] bytes = new byte[2]; 245 246 //第一个字节值在0xb0, 0xf7之间 247 bytes[0] = (byte)rndNumber.Next(0xb0, 0xf8); 248 //第二个字节值在0xa1, 0xfe之间 249 bytes[1] = (byte)rndNumber.Next(0xa1, 0xff); 250 251 //根据汉字编码的字节数组解码出中文汉字 252 string str1 = Encoding.GetEncoding("gb2312").GetString(bytes); 253 254 return str1[0]; 255 } 256 } 257 }
调用56短信王的短信接口
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.HtmlControls; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Xml.Linq; 11 using System.Drawing; 12 using System.Text; 13 14 /// <summary> 15 /// VdCode 的摘要说明 16 /// </summary> 17 public class VdCode 18 { 19 /// <summary> 20 /// 全局随机数生成器 21 /// </summary> 22 private Random rndNumber; 23 public static string mrChineseChars = String.Empty; 24 /// <summary> 25 /// 英文与数字串 26 /// </summary> 27 // protected static readonly string mrEnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 28 protected static readonly string mrEnglishOrNumChars = "0123456789"; 29 30 public VdCode() 31 { 32 rndNumber = new Random(unchecked((int)DateTime.Now.Ticks)); 33 } 34 // 验证码长度(默认6个验证码的长度) 35 int length = 5; 36 public int Length 37 { 38 get { return length; } 39 set { length = value; } 40 } 41 //验证码字体大小 42 // int fontSize = 18; 43 int fontSize = 18; 44 public int FontSize 45 { 46 get { return fontSize; } 47 set { fontSize = value; } 48 } 49 // 边框补(默认4像素) 50 //int padding = 4; 51 int padding = 4; 52 public int Padding 53 { 54 get { return padding; } 55 set { padding = value; } 56 } 57 // 是否输出燥点(默认输出) 58 bool chaos = true; 59 public bool Chaos 60 { 61 get { return chaos; } 62 set { chaos = value; } 63 } 64 // 输出燥点的颜色(默认灰色) 65 Color chaosColor = Color.LightGray; 66 public Color ChaosColor 67 { 68 get { return chaosColor; } 69 set { chaosColor = value; } 70 } 71 // 自定义背景色(默认白色) 72 Color backgroundColor = Color.White; 73 public Color BackgroundColor 74 { 75 get { return backgroundColor; } 76 set { backgroundColor = value; } 77 } 78 // 自定义随机颜色数组 79 Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 80 public Color[] Colors 81 { 82 get { return colors; } 83 set { colors = value; } 84 } 85 // 自定义字体数组 86 string[] fonts = { "Arial", "Georgia" }; 87 public string[] Fonts 88 { 89 get { return fonts; } 90 set { fonts = value; } 91 } 92 #region 产生扭曲图片 93 private const double PI = 3.1415926535897932384626433832795; 94 private 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> 105 public 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对象 112 double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;//判断扭曲方式 113 for (int i = 0; i < destBmp.Width; i++) 114 { 115 for (int j = 0; j < destBmp.Height; j++) 116 { 117 double dx = 0; 118 dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen; 119 dx += dPhase; 120 double dy = Math.Sin(dx); 121 // 取得当前点的颜色 122 int 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); 126 if (nOldX >= 0 && nOldX < destBmp.Width 127 && nOldY >= 0 && nOldY < destBmp.Height) 128 { 129 destBmp.SetPixel(nOldX, nOldY, color); 130 } 131 } 132 } 133 return destBmp; 134 } 135 #endregion 136 /// <summary> 137 /// 生成校验码图片 138 /// </summary> 139 /// <param name="code">验证码</param> 140 /// <returns></returns> 141 public Bitmap CreateImage(string code) 142 { 143 int fSize = FontSize; 144 int fWidth = fSize + Padding; 145 int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2; 146 int 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 //给背景添加随机生成的燥点 151 if (this.Chaos) 152 { 153 Pen pen = new Pen(ChaosColor, 0); 154 int c = Length * 10; 155 for (int i = 0; i < c; i++) 156 { 157 int x = rndNumber.Next(image.Width); 158 int y = rndNumber.Next(image.Height); 159 160 g.DrawRectangle(pen, x, y, 1, 1); 161 } 162 } 163 int left = 0, top = 0, top1 = 1, top2 = 1; 164 int n1 = (imageHeight - FontSize - Padding * 2); 165 int n2 = n1 / 4; 166 top1 = n2; 167 top2 = n2 * 2; 168 Font f; 169 Brush b; 170 int cindex, findex; 171 //随机字体和颜色的验证码字符 172 for (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]); 178 if (i % 2 == 1) 179 { 180 top = top2; 181 } 182 else 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.com) 193 //image = TwistImage(image, true, 8, 4); 194 image = TwistImage(image, false, 0, 0); 195 return image; 196 } 197 /// <summary> 198 /// 生成随机字符码 199 /// </summary> 200 /// <param name="codeLen">字符串长度</param> 201 /// <param name="zhCharsCount">中文字符数</param> 202 /// <returns></returns> 203 public string CreateVerifyCode(int codeLen, int zhCharsCount) 204 { 205 char[] chs = new char[codeLen]; 206 207 int index; 208 for (int i = 0; i < zhCharsCount; i++) 209 { 210 index = rndNumber.Next(0, codeLen); 211 if (chs[index] == '\0') 212 chs[index] = CreateZhChar(); 213 else 214 --i; 215 } 216 for (int i = 0; i < codeLen; i++) 217 { 218 if (chs[i] == '\0') 219 chs[i] = CreateEnOrNumChar(); 220 } 221 222 return new string(chs, 0, chs.Length); 223 } 224 // 生成默认长度5的随机字符码 225 public string CreateVerifyCode() 226 { 227 return CreateVerifyCode(Length, 0); 228 } 229 // 生成英文或数字字符 230 protected char CreateEnOrNumChar() 231 { 232 return mrEnglishOrNumChars[rndNumber.Next(0, mrEnglishOrNumChars.Length)]; 233 } 234 protected char CreateZhChar() // 生成汉字字符 235 { 236 //若提供了汉字集,查询汉字集选取汉字 237 if (mrChineseChars.Length > 0) 238 { 239 return mrChineseChars[rndNumber.Next(0, mrChineseChars.Length)]; 240 } 241 //若没有提供汉字集,则根据《GB2312简体中文编码表》编码规则构造汉字 242 else 243 { 244 byte[] bytes = new byte[2]; 245 246 //第一个字节值在0xb0, 0xf7之间 247 bytes[0] = (byte)rndNumber.Next(0xb0, 0xf8); 248 //第二个字节值在0xa1, 0xfe之间 249 bytes[1] = (byte)rndNumber.Next(0xa1, 0xff); 250 251 //根据汉字编码的字节数组解码出中文汉字 252 string str1 = Encoding.GetEncoding("gb2312").GetString(bytes); 253 254 return str1[0]; 255 } 256 } 257 }
调用方法验证用户是否输入正确
VdCode gc = new VdCode(); string googleCode = gc.CreateVerifyCode(4, 0);//生成汉字,只生成数字的,若有有字母的,修改一下类中的mrEnglishOrNumChars Session["VdCode"] = googleCode.ToString(); System.Drawing.Bitmap bitmap = gc.CreateImage(googleCode); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.Clear(); //Response.ContentType = "image/Png"; Response.BinaryWrite(ms.GetBuffer()); bitmap.Dispose(); ms.Dispose(); ms.Close(); Response.End();
【版权所有@ithuo】 【博客地址 http://www.cnblogs.com/ithuo】 可以转载,但请注明出处并保持博客超链接,有不正确的地方请不吝赐教,谢谢大家的帮助与支持!