ASP.NET中常用功能代码总结(2)——生成验证图片

ASP.NET中常用功能代码总结(2——生成验证图片<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

整理:Terrylee

生成随机的验证图片

使用方法:

1 ExpandedBlockStart.gif ContractedBlock.gif /**/ ///调用函数将验证码生成图片
2 None.gif              this .CreateCheckCodeImage(GenerateCheckCode());

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 产生五位的随机字符串
 3InBlock.gif        /// </summary>
 4ExpandedBlockEnd.gif        /// <returns></returns>

 5 None.gif          private   string  GenerateCheckCode()
 6 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {  
 7InBlock.gif            int number;
 8InBlock.gif            char code;
 9InBlock.gif            string checkCode = String.Empty;
10InBlock.gif
11InBlock.gif            System.Random random = new Random();
12InBlock.gif
13InBlock.gif            for (int i = 0; i < 5; i++)
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                number = random.Next();
16InBlock.gif
17InBlock.gif                if (number % 2 == 0)
18InBlock.gif                    code = (char)('0' + (char)(number % 10));
19InBlock.gif                else
20InBlock.gif                    code = (char)('A' + (char)(number % 26));
21InBlock.gif
22InBlock.gif                checkCode += code.ToString();
23ExpandedSubBlockEnd.gif            }

24InBlock.gif
25ExpandedSubBlockStart.gifContractedSubBlock.gif            /**////用于客户端校验码比较
26InBlock.gif            Session["CheckCode"= checkCode;
27InBlock.gif
28InBlock.gif            return checkCode;
29ExpandedBlockEnd.gif        }

30 None.gif        
31 ExpandedBlockStart.gifContractedBlock.gif         /**/ /// <summary>
32InBlock.gif        /// 将验证码生成图片显示
33InBlock.gif        /// </summary>
34ExpandedBlockEnd.gif        /// <param name="checkCode"></param>

35 None.gif          private   void  CreateCheckCodeImage( string  checkCode)
36 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {  
37InBlock.gif            if (checkCode == null || checkCode.Trim() == String.Empty)
38InBlock.gif                return;
39InBlock.gif
40InBlock.gif            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
41InBlock.gif            Graphics g = Graphics.FromImage(image);
42InBlock.gif
43InBlock.gif            try
44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
45ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////生成随机生成器 
46InBlock.gif                Random random = new Random();
47InBlock.gif
48ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////清空图片背景色 
49InBlock.gif                g.Clear(Color.White);
50InBlock.gif
51ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////画图片的背景噪音线 
52InBlock.gif                for (int i = 0; i < 25; i++)
53ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
54InBlock.gif                    int x1 = random.Next(image.Width);
55InBlock.gif                    int x2 = random.Next(image.Width);
56InBlock.gif                    int y1 = random.Next(image.Height);
57InBlock.gif                    int y2 = random.Next(image.Height);
58InBlock.gif
59InBlock.gif                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
60ExpandedSubBlockEnd.gif                }

61InBlock.gif
62InBlock.gif                Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
63InBlock.gif                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
64InBlock.gif                g.DrawString(checkCode, font, brush, 22);
65InBlock.gif
66ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////画图片的前景噪音点 
67InBlock.gif                for (int i = 0; i < 100; i++)
68ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
69InBlock.gif                    int x = random.Next(image.Width);
70InBlock.gif                    int y = random.Next(image.Height);
71InBlock.gif
72InBlock.gif                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
73ExpandedSubBlockEnd.gif                }

74InBlock.gif
75ExpandedSubBlockStart.gifContractedSubBlock.gif                /**////画图片的边框线 
76InBlock.gif                g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
77InBlock.gif
78InBlock.gif                System.IO.MemoryStream ms = new System.IO.MemoryStream();
79InBlock.gif                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
80InBlock.gif                Response.ClearContent();
81InBlock.gif                Response.ContentType = "image/Gif";
82InBlock.gif                Response.BinaryWrite(ms.ToArray());
83ExpandedSubBlockEnd.gif            }

84InBlock.gif            finally
85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
86InBlock.gif                g.Dispose();
87InBlock.gif                image.Dispose();
88ExpandedSubBlockEnd.gif            }

89ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NET图片加减法的验证码控件,、 this.CreateCheckCodeImage(GenerateCheckCode()); private string GenerateCheckCode() { int intFirst, intSec, intTemp; string checkCode = String.Empty; System.Random random = new Random(); intFirst = random.Next(1, 10); intSec = random.Next(1, 10); switch (random.Next(1, 3).ToString()) { case "2": if (intFirst < intSec) { intTemp = intFirst; intFirst = intSec; intSec = intTemp; } checkCode = "=" + intFirst + "-" + intSec; Session["ValidCode"] = intFirst - intSec; break; default: checkCode = "=" + intFirst + "+" + intSec; Session["ValidCode"] = intFirst + intSec; break; } //Response.Cookies.Add(new HttpCookie("ValidCode",Movie.Common.AES.EncryptAes(checkCode))); return checkCode; } #region 产生波形滤镜效果 private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; private System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) { System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height); // 将位图背景填充为白色 System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp); graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height); graph.Dispose(); double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width; for (int i = 0; i < destBmp.Width; i++) { for (int j = 0; j < destBmp.Height; j++) { double dx = 0; dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen; dx += dPhase; double dy = Math.Sin(dx); // 取得当前点的颜色 int nOldX = 0, nOldY = 0; nOldX = bXDir ? i + (int)(dy * dMultValue) : i; nOldY = bXDir ? j : j + (int)(dy * dMultValue); System.Drawing.Color color = srcBmp.GetPixel(i, j); if (nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height) { destBmp.SetPixel(nOldX, nOldY, color); } } } return destBmp; } #endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值