Exemplo n.º 1
0
        public static byte[] VisualCaptcha(string source)
        {
            try
            {
                System.Random r = new System.Random();
                int           w = 350, h = 75;
                string        family = "Arial Rounded MT Bold";
                using (var bmp = new System.Drawing.Bitmap(w, h
                                                           , System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    int m = 0, nm = 0;
                    System.Drawing.Color tc;
                    using (var g = System.Drawing.Graphics.FromImage(bmp))
                    {
                        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                        g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(System.Drawing.Color.White);
                        System.Drawing.SizeF size;
                        m    = r.Next() % 9 + 1;
                        nm   = r.Next() % 3;
                        tc   = System.Drawing.Color.FromArgb(255, 255, 255);
                        size = g.MeasureString(source
                                               , new System.Drawing.Font(family, h * 1.2f, System.Drawing.FontStyle.Bold)
                                               , new System.Drawing.SizeF(w * 1F, h * 1F));
                        using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                                   new System.Drawing.Rectangle(0, 0, w, h)
                                   , System.Drawing.Color.Black
                                   , System.Drawing.Color.Black, 45, false))
                        {
                            System.Drawing.Drawing2D.ColorBlend blend =
                                new System.Drawing.Drawing2D.ColorBlend(6);
                            for (var i = 0; i < 6; i++)
                            {
                                blend.Positions[i] = i * (1 / 5F); blend.Colors[i] = r.RandomColor(255, 64, 128);
                            }
                            brush.InterpolationColors = blend;

                            for (int wave = 0; wave < 2; wave++)
                            {
                                int min = (15 + wave * 20);
                                System.Drawing.PointF[] pt = new System.Drawing.PointF[] {
                                    new System.Drawing.PointF(16f, (float)r.Next(min, min + 10))
                                    , new System.Drawing.PointF(240f, (float)r.Next(min + 10, min + 20))
                                };
                                System.Collections.Generic.List <System.Drawing.PointF> PointList =
                                    new System.Collections.Generic.List <System.Drawing.PointF>();

                                float curDist = 0, distance = 0;
                                for (int i = 0; i < pt.Length - 1; i++)
                                {
                                    System.Drawing.PointF ptA = pt[i], ptB = pt[i + 1];
                                    float deltaX = ptB.X - ptA.X, deltaY = ptB.Y - ptA.Y;
                                    curDist  = 0;
                                    distance = (float)System.Math.Sqrt(System.Math.Pow(deltaX, 2) + System.Math.Pow(deltaY, 2));
                                    while (curDist < distance)
                                    {
                                        curDist++;
                                        float offsetX = (float)((double)curDist / (double)distance * (double)deltaX);
                                        float offsetY = (float)((double)curDist / (double)distance * (double)deltaY);
                                        PointList.Add(new System.Drawing.PointF(ptA.X + offsetX, ptA.Y + offsetY));
                                    }
                                }
                                for (int i = 0; i < PointList.Count - 24; i = i + 24)
                                {
                                    float x1 = PointList[i].X, y1 = PointList[i].Y, x2 = PointList[i + 24].X, y2 = PointList[i + 24].Y;
                                    float angle = (float)((System.Math.Atan2(y2 - y1, x2 - x1) * 180 / 3.14159265));
                                    g.TranslateTransform(x1, y1);
                                    g.RotateTransform(angle);
                                    int pm = r.Next() % 2 + 1;
                                    System.Drawing.Point[] p1 = new System.Drawing.Point[] {
                                        new System.Drawing.Point(0, 0)
                                        , new System.Drawing.Point(3, -3 * pm)
                                        , new System.Drawing.Point(6, -4 * pm)
                                        , new System.Drawing.Point(9, -3 * pm)
                                        , new System.Drawing.Point(12, 0)
                                        , new System.Drawing.Point(15, 3 * pm)
                                        , new System.Drawing.Point(18, 4 * pm)
                                        , new System.Drawing.Point(21, 3 * pm)
                                        , new System.Drawing.Point(24, 0)
                                    };
                                    using (var path = new System.Drawing.Drawing2D.GraphicsPath()) g.DrawLines(new System.Drawing.Pen(brush, 2f), p1);
                                    g.RotateTransform(-angle);
                                    g.TranslateTransform(-x1, -y1);
                                }
                            }
                            using (var path = new System.Drawing.Drawing2D.GraphicsPath())
                            {
                                System.Drawing.PointF[] points = new System.Drawing.PointF[] { };
                                if (m == 1 || m == 2 || m == 3) // star trek inverse
                                {
                                    path.AddString(source
                                                   , new System.Drawing.FontFamily(family), 1, h * 0.75F
                                                   , new System.Drawing.PointF((w - size.Width) / 2F
                                                                               , (h * 0.9F - size.Height) / 2F)
                                                   , System.Drawing.StringFormat.GenericTypographic);
                                    points = new System.Drawing.PointF[] {
                                        new System.Drawing.PointF(0, 0)
                                        , new System.Drawing.PointF(w, 0)
                                        , new System.Drawing.PointF(w * 0.2F, h)
                                        , new System.Drawing.PointF(w * 0.8F, h)
                                    };
                                }
                                else if (m == 4 || m == 5) // star trek
                                {
                                    path.AddString(source, new System.Drawing.FontFamily(family), 1, h * 0.75F
                                                   , new System.Drawing.PointF((w - size.Width) / 2F
                                                                               , (h * 1.2F - size.Height) / 2F + 2F)
                                                   , System.Drawing.StringFormat.GenericTypographic);
                                    points = new System.Drawing.PointF[] { new System.Drawing.PointF(w * 0.2F, 0)
                                                                           , new System.Drawing.PointF(w * 0.8F, 0)
                                                                           , new System.Drawing.PointF(0, h)
                                                                           , new System.Drawing.PointF(w, h) };
                                }
                                else if (m == 6 || m == 7) // grow from left
                                {
                                    path.AddString(source, new System.Drawing.FontFamily(family), 1, h * 0.75F
                                                   , new System.Drawing.PointF((w * 1.15F - size.Width) / 2F
                                                                               , (h - size.Height) / 2F)
                                                   , System.Drawing.StringFormat.GenericTypographic);
                                    points = new System.Drawing.PointF[] {
                                        new System.Drawing.PointF(0, h * 0.25F)
                                        , new System.Drawing.PointF(w, 0)
                                        , new System.Drawing.PointF(0, h * 0.75F)
                                        , new System.Drawing.PointF(w, h)
                                    };
                                }
                                else if (m == 8 || m == 9) // grow from right
                                {
                                    path.AddString(source, new System.Drawing.FontFamily(family), 1, h * 0.75F
                                                   , new System.Drawing.PointF((w * 0.85F - size.Width) / 2F
                                                                               , (h - size.Height) / 2F)
                                                   , System.Drawing.StringFormat.GenericTypographic);

                                    points = new System.Drawing.PointF[] {
                                        new System.Drawing.PointF(w * 0.1F, 0)
                                        , new System.Drawing.PointF(w * 0.9F, h * 0.25F)
                                        , new System.Drawing.PointF(w * 0.1F, h)
                                        , new System.Drawing.PointF(w * 0.9F, h * 0.75F)
                                    };
                                }
                                path.Warp(points, new System.Drawing.RectangleF(0, 0, w, h));
                                g.FillPath(System.Drawing.Brushes.White, path);
                                g.DrawPath(new System.Drawing.Pen(brush, 2F), path);
                            }
                        }
                    }
                    using (var thumb = new System.Drawing.Bitmap(128, 40
                                                                 , System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    {
                        using (var g = System.Drawing.Graphics.FromImage(thumb))
                        {
                            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                            g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            System.Drawing.Rectangle tr = new System.Drawing.Rectangle(0, 0, thumb.Width, thumb.Height);
                            g.DrawImage(bmp, tr);
                            g.DrawRectangle(
                                new System.Drawing.Pen(System.Drawing.Brushes.White)
                                , new System.Drawing.Rectangle(0, 0, 127, 39));
                        }
                        using (var ms = new System.IO.MemoryStream())
                        {
                            ((System.Drawing.Image)thumb).Save(ms
                                                               , System.Drawing.Imaging.ImageFormat.Png);
                            return(ms.ToArray());
                        }
                    }
                }
            }
            catch { return(null); }
        }