GDI+畫曲線外型的變形文字(可用於圖形驗證碼)

來源:互聯網
上載者:User

先看效果:

關鍵代碼:

        private void DrawWords(Graphics graphics, int width, int height, string phrase)
        {
            graphics.FillRectangle(Brushes.White, 0, 0, width, height);

            using (var gp = new GraphicsPath())
            {
                gp.AddString(phrase,new FontFamily("Arial"), (int)FontStyle.Bold, 48f, new Point(0, 0), StringFormat.GenericDefault);

                using (var gpp = GraphicsPathDeform(gp, width, height))
                {
                    var bounds = gpp.GetBounds();
                    var matrix = new Matrix();
                    var x = (width - bounds.Width) / 2 - bounds.Left;
                    var y = (height - bounds.Height) / 2 - bounds.Top;
                    matrix.Translate(x, y);
                    gpp.Transform(matrix);
                    graphics.FillPath(Brushes.Black, gpp);
                }
            }
            graphics.Flush();
        }

        private GraphicsPath GraphicsPathDeform(GraphicsPath path, int width, int height)
        {
            var WarpFactor = 3;
            var xAmp = WarpFactor * width / 100d;
            var yAmp = WarpFactor * height / 50d;
            var xFreq = 2d * Math.PI / width;
            var yFreq = 2d * Math.PI / height;
            Random rng = new Random();
            var deformed = new PointF[path.PathPoints.Length];
            var xSeed = rng.NextDouble() * 2 * Math.PI;
            var ySeed = rng.NextDouble() * 2 * Math.PI;
            var i = 0;
            foreach (var original in path.PathPoints)
            {
                var val = xFreq * original.X + yFreq * original.Y;
                var xOffset = (int)(xAmp * Math.Sin(val + xSeed));
                var yOffset = (int)(yAmp * Math.Sin(val + ySeed));
                deformed[i++] = new PointF(original.X + xOffset,original.Y + yOffset);
            }

            return new GraphicsPath(deformed, path.PathTypes);

        }

測試:

        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(600, 600);
            Graphics g = Graphics.FromImage(bmp);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            DrawWords(g, 500, 100, "Guangdong, China");
            pictureBox1.Image = bmp;
            g.Dispose();
        }

當然,如果你認為上面的還不夠驗證要求,還可以加上背景雜色、背景圖片或者不規則的線條、曲線等,也可以先將上面的文字進行旋轉等變形後,再做上述處理,但有一個大原則:要方便人眼識別,而程式無法識別或很難識別。這裡就不再詳述。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.