ASP.NET C# 驗證碼 支援中文 噪點 彎曲

來源:互聯網
上載者:User
/// <summary>
        /// 根據指定參數返回BitMap對象
        /// 引用如下:
        /// using System.Drawing;
        /// 調用例子如下:
        ///     eg1、儲存為圖象檔案為
        ///     Bitmap srBmp = srBitmap(srs);
        ///     srBmp.Save(Directory.GetCurrentDirectory() + "\\srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
        ///     srBmp.Dispose();
        ///     eg2。網頁中調用方式如下
        ///     Bitmap srBmp = srBitmap(srs);
        ///     System.IO.MemoryStream srMS = new System.IO.MemoryStream();
        ///     srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
        ///     Response.ClearContent();
        ///     Response.ContentType = "image/gif";
        ///     Response.BinaryWrite(srMS.ToArray());
        ///     srBmp.Dispose();
        /// </summary>
        /// <param name="srs"></param>
        /// <returns></returns>

        public static Bitmap srBitmap(string srs)
        {
            //定義圖片彎曲的角度
            int srseedangle = 45;
            //定義圖象
            Bitmap srBmp = new Bitmap(srs.Length*20,30);
            //畫圖
            Graphics srGraph = Graphics.FromImage(srBmp);
            //清空圖象
            srGraph.Clear(Color.AliceBlue);
            //給圖象畫邊框
            srGraph.DrawRectangle(new Pen(Color.Black,0),0,0,srBmp.Width-1,srBmp.Height-1);
            //定義隨即數
            Random srRandom = new Random();
            //定義畫筆
            Pen srPen = new Pen(Color.LightGray,0);
            //畫噪點
            for (int i = 0; i < 100; i++)
            {
                srGraph.DrawRectangle(srPen,srRandom.Next(1,srBmp.Width-2),srRandom.Next(1,srBmp.Height-2),1,1);
            }
            //將字串轉化為字元數組
            char[] srchars = srs.ToCharArray();
            //封狀文本
            StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
            //設定文本垂直置中
            srFormat.Alignment = StringAlignment.Center;
            //設定文本水平置中
            srFormat.LineAlignment = StringAlignment.Center;
            //定義字型顏色
           
Color[] srColors ={
Color.Black,Color.Red,Color.DarkBlue,Color.Blue,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
            //定義字型
            string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };
            //迴圈畫出每個字元
            for (int i = 0, j = srchars.Length; i < j; i++)
            {
                //定義字型 參數分別為字型樣式 字型大小 字型字形
                Font srFont = new Font(srFonts[srRandom.Next(5)], srRandom.Next(12,20), FontStyle.Regular);
                //填充圖形
                Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
                //定義座標
                Point srPoint = new Point(16, 16);
                //定義傾斜角度
                float srangle = srRandom.Next(-srseedangle, srseedangle);
                //傾斜
                srGraph.TranslateTransform(srPoint.X, srPoint.Y);
                srGraph.RotateTransform(srangle);
                //填充字元
                srGraph.DrawString(srchars[i].ToString(),srFont,srBrush,1,1,srFormat);
                //迴歸正常
                srGraph.RotateTransform(-srangle);
                srGraph.TranslateTransform(2,-srPoint.Y);
            }
            srGraph.Dispose();
            return srBmp;
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.