zoukankan      html  css  js  c++  java
  • 漂亮的验证码

    using System.Text;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;

      protected void Page_Load(object sender, EventArgs e)
            {
                
                String sCode = String.Empty;
                //清除該頁輸出緩存,設置該頁無緩存
                Response.Buffer = true;
                Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
                Response.Expires = 0;
                Response.CacheControl = "no-cache";
                Response.AppendHeader("Pragma", "No-Cache");

                // '將驗證碼圖片寫入記憶體流,並將其以 "image/Png" 格式輸出
                MemoryStream oStream = new MemoryStream();
                try
                {
                    CreateValidateCodeImage(oStream, sCode, 4, 100, 40, 18);
                    Session["_ValidateCode"] = sCode;
                    Response.ClearContent();
                    Response.ContentType = "image/Png";
                    Response.BinaryWrite(oStream.ToArray());
                }
                finally
                {
                    // '釋放資源
                    oStream.Dispose();
                }
            }
            /// <summary>
            /// 產生圖形驗證碼。
            /// </summary>
            /// <param name="MemoryStream">記憶體資料流。</param>
            /// <param name="Code">傳出驗證碼。</param>
            /// <param name="CodeLength">驗證碼字元數。</param>
            /// <param name="Width"></param>
            /// <param name="Height"></param>
            /// <param name="FontSize"></param>
            public void CreateValidateCodeImage(MemoryStream MemoryStream, String Code, int CodeLength, int Width, int Height, int FontSize)
            {
                Bitmap oBmp;
                oBmp = CreateValidateCodeImage(Code, CodeLength, Width, Height, FontSize);
                try
                {
                    oBmp.Save(MemoryStream, ImageFormat.Png);
                }
                finally { oBmp.Dispose(); }
            }
            /// <summary>
            /// 產生圖形驗證碼。
            /// </summary>
            /// <param name="Code">傳出驗證碼。</param>
            /// <param name="CodeLength">驗證碼字元數。</param>
            /// <param name="Width"></param>
            /// <param name="Height"></param>
            /// <param name="FontSize"></param>
            /// <returns></returns>
            public Bitmap CreateValidateCodeImage(String Code, int CodeLength, int Width, int Height, int FontSize)
            {
                String sCode = String.Empty;
                //顏色列表,用於驗證碼、噪線、噪點
                Color[] oColors ={
                 System.Drawing.Color.Black,
                 System.Drawing.Color.Red,
                 System.Drawing.Color.Blue,
                 System.Drawing.Color.Green,
                 System.Drawing.Color.Orange,
                 System.Drawing.Color.Brown,
                 System.Drawing.Color.Brown,
                 System.Drawing.Color.DarkBlue
                };
                //字體列表,用於驗證碼
                string[] oFontNames ={ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
                //驗證碼的字元集,去掉了一些容易混淆的字元
                char[] oCharacter = {
       '2',
       '3',
       '4',
       '5',
       '6',
       '8',
       '9',
       'A',
       'B',
       'C',
       'D',
       'E',
       'F',
       'G',
       'H',
       'J',
       'K',
       'L',
       'M',
       'N',
       'P',
       'R',
       'S',
       'T',
       'W',
       'X',
       'Y'
      };
                Random oRnd = new Random();
                Bitmap oBmp = null;
                Graphics oGraphics = null;
                int N1 = 0;
                System.Drawing.Point oPoint1 = default(System.Drawing.Point);
                System.Drawing.Point oPoint2 = default(System.Drawing.Point);
                string sFontName = null;
                Font oFont = null;
                Color oColor = default(Color);

                //生成驗證碼字串
                for (N1 = 0; N1 <= CodeLength - 1; N1++)
                {
                    sCode += oCharacter[oRnd.Next(oCharacter.Length)];
                }

                oBmp = new Bitmap(Width, Height);
                oGraphics = Graphics.FromImage(oBmp);
                oGraphics.Clear(System.Drawing.Color.White);
                try
                {
                    for (N1 = 0; N1 <= 4; N1++)
                    {
                        //畫噪線
                        oPoint1.X = oRnd.Next(Width);
                        oPoint1.Y = oRnd.Next(Height);
                        oPoint2.X = oRnd.Next(Width);
                        oPoint2.Y = oRnd.Next(Height);
                        oColor = oColors[oRnd.Next(oColors.Length)];
                        oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
                    }

                    for (N1 = 0; N1 <= sCode.Length - 1; N1++)
                    {
                        //畫驗證碼字串
                        sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
                        oFont = new Font(sFontName, FontSize, FontStyle.Italic);
                        oColor = oColors[oRnd.Next(oColors.Length)];
                        oGraphics.DrawString(sCode[N1].ToString(), oFont, new SolidBrush(oColor), Convert.ToSingle(N1) * FontSize + 10, Convert.ToSingle(8));
                    }

                    for (int i = 0; i <= 30; i++)
                    {
                        //畫噪點
                        int x = oRnd.Next(oBmp.Width);
                        int y = oRnd.Next(oBmp.Height);
                        Color clr = oColors[oRnd.Next(oColors.Length)];
                        oBmp.SetPixel(x, y, clr);
                    }

                    Code = sCode;
                    return oBmp;
                }
                finally
                {
                    oGraphics.Dispose();
                }


            }

    =====================================

    改编自博友的原代码为C#

    http://www.cnblogs.com/jeff377/archive/2008/10/29/1322471.html

    效果图如下:

    下载地址 /Files/ggbbeyou/改编验证码.rar

  • 相关阅读:
    vim删除以#,空格开头的行
    Element-ui 中对表单进行验证
    VUE页面实现加载外部HTML方法
    vue-cli2嵌入html
    文字环绕图片
    LocalDate计算两个日期相差天数
    springboot+vue脚手架使用nginx前后端分离
    通过 Netty、ZooKeeper 手撸一个 RPC 服务
    Spring Native 项目,把 Spring 项目编译成原生程序!
    印象笔记吐槽
  • 原文地址:https://www.cnblogs.com/ggbbeyou/p/1805879.html
Copyright © 2011-2022 走看看