Exemplo n.º 1
0
        private void ImageYarat()
        {
            this.familyName = System.Drawing.FontFamily.GenericSerif.Name;

            // 32bit bmp yaratalim
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(this.m_width, this.m_height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            // cizim icin bir graphics nesnesi yaratalim.
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Rectangle rec = new System.Drawing.Rectangle(0, 0, this.m_width, this.m_height);
            // Resimde arka alani dolduralim
            System.Drawing.Drawing2D.HatchBrush hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.White);
            g.FillRectangle(hb, rec);
            System.Drawing.Font font = new System.Drawing.Font(this.familyName, this.FontSize, System.Drawing.FontStyle.Bold);
            // textin formatini belirleyelim
            System.Drawing.StringFormat format = new System.Drawing.StringFormat();
            format.Alignment     = System.Drawing.StringAlignment.Center;
            format.LineAlignment = System.Drawing.StringAlignment.Center;
            // bir path belirleyip, texte random olarak aci verelim
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(this.m_text, font.FontFamily, (int)font.Style, font.Size, rec, format);
            float v = 4;

            System.Drawing.PointF[]         p      = { new System.Drawing.PointF(this.random.Next(rec.Width) / v, this.random.Next(rec.Height) / v), new System.Drawing.PointF(rec.Width - this.random.Next(rec.Width) / v, this.random.Next(rec.Height) / v), new System.Drawing.PointF(this.random.Next(rec.Width) / v, rec.Height - this.random.Next(rec.Height) / v), new System.Drawing.PointF(rec.Width - this.random.Next(rec.Width) / v, rec.Height - this.random.Next(rec.Height) / v) };
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0f, 0f);
            path.Warp(p, rec, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0);
            hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.LargeConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.Blue);
            g.FillPath(hb, path);
            for (int i = 0; i <= (int)(rec.Width * rec.Height / 30) - 1; i++)
            {
                int x = this.random.Next(rec.Width);
                int y = this.random.Next(rec.Height);
                int w = this.random.Next(rec.Width / 50);
                int h = this.random.Next(rec.Width / 40);
                g.FillEllipse(hb, x, y, w, h);
            }
            this.m_image = bitmap;
        }
Exemplo n.º 2
0
    private void GenerateImage()
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap
                                           (this.width, this.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        System.Drawing.Rectangle            rect       = new System.Drawing.Rectangle(0, 0, this.width, this.height);
        System.Drawing.Drawing2D.HatchBrush hatchBrush =
            new System.Drawing.Drawing2D.HatchBrush(
                System.Drawing.Drawing2D.HatchStyle.SmallConfetti,
                System.Drawing.Color.LightGray, System.Drawing.Color.White);
        g.FillRectangle(hatchBrush, rect);
        System.Drawing.SizeF size;
        float fontSize = rect.Height + 1;

        System.Drawing.Font font;

        do
        {
            fontSize--;
            font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, fontSize, System.Drawing.FontStyle.Bold);
            size = g.MeasureString(this.text, font);
        } while (size.Width > rect.Width);
        System.Drawing.StringFormat format = new System.Drawing.StringFormat();
        format.Alignment     = System.Drawing.StringAlignment.Center;
        format.LineAlignment = System.Drawing.StringAlignment.Center;
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        //path.AddString(this.text, font.FontFamily, (int) font.Style,
        //    font.Size, rect, format);
        path.AddString(this.text, font.FontFamily, (int)font.Style, 75, rect, format);
        float v = 4F;

        System.Drawing.PointF[] points =
        {
            new System.Drawing.PointF(this.random.Next(rect.Width) / v, this.random.Next(
                                          rect.Height) / v),
            new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v,
                                      this.random.Next(rect.Height) / v),
            new System.Drawing.PointF(this.random.Next(rect.Width) / v,
                                      rect.Height - this.random.Next(rect.Height) / v),
            new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v,
                                      rect.Height - this.random.Next(rect.Height) / v)
        };
        System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
        matrix.Translate(0F, 0F);
        path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);
        hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
            System.Drawing.Drawing2D.HatchStyle.Percent10
            , System.Drawing.Color.Black, System.Drawing.Color.SkyBlue);
        g.FillPath(hatchBrush, path);
        int m = System.Math.Max(rect.Width, rect.Height);

        for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
        {
            int x = this.random.Next(rect.Width);
            int y = this.random.Next(rect.Height);
            int w = this.random.Next(m / 50);
            int h = this.random.Next(m / 50);
            g.FillEllipse(hatchBrush, x, y, w, h);
        }
        font.Dispose();
        hatchBrush.Dispose();
        g.Dispose();
        this.image = bitmap;
    }
Exemplo n.º 3
0
        public Stream GetLetter(LetterOptions options)
        {
            _stream = new MemoryStream();
            using (var renderer = _renderer)
            {
                var result = renderer.Configure(o =>
                {
                    o.Width  = options.Width;
                    o.Height = options.Height;
                })
                             .Render(bitmap =>
                {
                    // Create a graphics object for drawing.
                    var g           = Graphics.FromImage(bitmap);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    var rect        = new RectangleF(0, 0, options.Width, options.Height);

                    // Fill in the background.

                    var backBrush = new System.Drawing.Drawing2D.LinearGradientBrush(
                        rect,
                        options.BackColor.ToColor(Color.CornflowerBlue),
                        options.BackColor.ToColor(Color.CornflowerBlue),
                        System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
                    g.FillRectangle(backBrush, rect);

                    // create solid dark brush
                    var gradBrushDark = new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.Black,
                                                                                         Color.Gray,
                                                                                         System.Drawing.Drawing2D.LinearGradientMode.Horizontal);

                    // Set up the text font.
                    SizeF size;
                    float fontSize = 200;
                    Font font      = null;
                    // Adjust the font size until the text fits within the image.
                    do
                    {
                        fontSize -= 1;
                        font      = new Font(options.FontFamily, fontSize, FontStyle.Bold);
                        size      = g.MeasureString(options.Letter.ToString(), font);
                    } while (size.Width > rect.Width);

                    // Set up the text format.
                    var format           = new StringFormat();
                    format.Alignment     = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;

                    // Create a path using the text and warp it randomly - nice :)
                    var path = new System.Drawing.Drawing2D.GraphicsPath();
                    path.AddString(options.Letter.ToString(),
                                   font.FontFamily,
                                   System.Convert.ToInt32(font.Style),
                                   font.Size,
                                   rect,
                                   format);
                    float v         = 8f;
                    PointF[] points =
                    {
                        new PointF((Int32)rect.Width / v,              (Int32)rect.Height / v),
                        new PointF(rect.Width - (Int32)rect.Width / v, (Int32)rect.Height / v),
                        new PointF((Int32)rect.Width / v,              rect.Height - (Int32)rect.Height / v),
                        new PointF(rect.Width - (Int32)rect.Width / v, rect.Height - (Int32)rect.Height / v)
                    };

                    var matrix = new System.Drawing.Drawing2D.Matrix();
                    matrix.Translate(0f, 0f);
                    path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0f);

                    // Draw the letter
                    var foreBrush = new System.Drawing.Drawing2D.LinearGradientBrush(
                        rect,
                        Color.White,
                        Color.White,
                        System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
                    g.FillPath(foreBrush, path);
                });

                result.Save(_stream, ImageFormat.Png);
            }

            return(_stream);
        }
Exemplo n.º 4
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); }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Обрабатывает запрос на тестовую картинку с заданными размерами (Width and Height)
        /// </summary>
        public System.Web.Mvc.ActionResult GetImage(System.String w, System.String h)
        {
            // parse args
            var width = System.Int32.Parse(w);

            if (width <= 0)
            {
                throw new System.ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
            }
            var height = System.Int32.Parse(h);

            if (height <= 0)
            {
                throw new System.ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
            }
            var text   = "";
            var random = new System.Random();

            for (var i = 0; i < 6; i++)
            {
                text += random.Next(10);
            }
            // keep captcha key in session
            this.Session["CAPTCHA"] = text;
            // Create a CAPTCHA image and send it as response
            System.Drawing.Bitmap image = null;
            try
            {
                // Create a new 32-bit bitmap image.
                image = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                // Create a graphics object for drawing.
                var g = System.Drawing.Graphics.FromImage(image);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                var rect = new System.Drawing.Rectangle(0, 0, width, height);
                // Fill in the background.
                var hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.White);
                g.FillRectangle(hatchBrush, rect);
                // Set up the text font.
                System.Drawing.SizeF size;
                var fontSize = rect.Height + 1;
                System.Drawing.Font font;
                // Adjust the font size until the text fits within the image.
                do
                {
                    fontSize--;
                    font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif.Name, fontSize, System.Drawing.FontStyle.Bold);
                    size = g.MeasureString(text, font);
                } while (size.Width > rect.Width);
                // Set up the text format.
                var format = new System.Drawing.StringFormat();
                format.Alignment     = System.Drawing.StringAlignment.Center;
                format.LineAlignment = System.Drawing.StringAlignment.Center;
                // Create a path using the text and warp it randomly.
                var path = new System.Drawing.Drawing2D.GraphicsPath();
                path.AddString(text, font.FontFamily, (System.Int32)font.Style, font.Size, rect, format);
                var v      = 4F;
                var points = new System.Drawing.PointF[]
                {
                    new System.Drawing.PointF(random.Next(rect.Width) / v, random.Next(rect.Height) / v),
                    new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, random.Next(rect.Height) / v),
                    new System.Drawing.PointF(random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v),
                    new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v)
                };
                var matrix = new System.Drawing.Drawing2D.Matrix();
                matrix.Translate(0F, 0F);
                path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);
                // Draw the text.
                hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.LargeConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.DarkGray);
                g.FillPath(hatchBrush, path);
                // Add some random noise.
                int m = System.Math.Max(rect.Width, rect.Height);
                for (var i = 0; i < (System.Int32)(rect.Width * rect.Height / 30F); i++)
                {
                    g.FillEllipse(hatchBrush, random.Next(rect.Width), random.Next(rect.Height), random.Next(m / 50), random.Next(m / 50));
                }
                // Clean up.
                font.Dispose();
                hatchBrush.Dispose();
                g.Dispose();
                // send image response
                var converter = new System.Drawing.ImageConverter();
                return(this.File((System.Byte[])converter.ConvertTo(image, typeof(System.Byte[])), "image/jpeg"));
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }
Exemplo n.º 6
0
        protected void GenerateImage()
        {
            Random random = new Random();
            // Create a new 32-bit bitmap image.
            Bitmap bitmap = new Bitmap(230,37,PixelFormat.Format32bppArgb);

            // Create a graphics object for drawing.
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle rect = new Rectangle(0, 0, 230, 37);

            // Fill in the background.
            System.Drawing.Drawing2D.HatchBrush hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50, Color.LightGray, Color.White);
            //SolidBrush hatchBrush = new SolidBrush(Color.Red);

            String strText =(String)Session["ImageSID"];
            g.FillRectangle(hatchBrush, rect);

            // Set up the text font.
            SizeF size;
            float fontSize = rect.Height -12;
            // Adjust the font size until the text fits within the image.
             Font font = new Font("Century Schoolbook",fontSize, FontStyle.Bold);
                size = g.MeasureString(strText, font);
            // Set up the text format.
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Create a path using the text and warp it randomly.
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(
              strText,
              font.FontFamily,
              (int)font.Style,
              font.Size, rect,
              format);
            float v = 12F;
            PointF[] points =
              {
            new PointF(
              random.Next(rect.Width) / v,
              random.Next(rect.Height) / v),
            new PointF(
              rect.Width - random.Next(rect.Width) / v,
              random.Next(rect.Height) / v),
            new PointF(
              random.Next(rect.Width) / v,
              rect.Height - random.Next(rect.Height) / v),
            new PointF(
              rect.Width - random.Next(rect.Width) / v,
              rect.Height - random.Next(rect.Height) / v)
              };
            //points={0,0,0,0};
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0F, 0F);
            path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);

            // Draw the text.
            hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
              System.Drawing.Drawing2D.HatchStyle.Percent50,
              Color.LightSlateGray,
              Color.DarkGray);
            g.FillPath(hatchBrush, path);

            // Add some random noise.
            int m = Math.Max(rect.Width, rect.Height);
            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = random.Next(rect.Width);
                int y = random.Next(rect.Height);
                int w = random.Next(m / 50);
                int h = random.Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }

            // Clean up.
            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();

            // Set the image.
            this.Response.Clear();
            this.Response.ContentType = "image/jpeg";

            // Write the image to the response stream in JPEG format.
            bitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);

            // Dispose of the CAPTCHA image object.
            bitmap.Dispose();
        }
Exemplo n.º 7
0
        // ====================================================================
        // Creates the bitmap image.
        // ====================================================================
        private void GenerateImage()
        {
            // Create a new 32-bit bitmap image.
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(this.width, this.height
                                                                     , System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // Create a graphics object for drawing.
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, this.width, this.height);

            // Fill in the background.
            System.Drawing.Drawing2D.HatchBrush hatchBrush =
                new System.Drawing.Drawing2D.HatchBrush(
                    System.Drawing.Drawing2D.HatchStyle.SmallConfetti
                    , System.Drawing.Color.LightGray
                    , System.Drawing.Color.White);
            g.FillRectangle(hatchBrush, rect);

            // Set up the text font.
            System.Drawing.SizeF size;
            float fontSize = rect.Height + 1;

            System.Drawing.Font font;
            // Adjust the font size until the text fits within the image.
            do
            {
                fontSize--;
                font = new System.Drawing.Font(this.familyName, fontSize, System.Drawing.FontStyle.Bold);
                size = g.MeasureString(this.text, font);
            } while (size.Width > rect.Width);

            // Set up the text format.
            System.Drawing.StringFormat format = new System.Drawing.StringFormat();
            format.Alignment     = System.Drawing.StringAlignment.Center;
            format.LineAlignment = System.Drawing.StringAlignment.Center;

            // Create a path using the text and warp it randomly.
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
            float v = 4F;

            System.Drawing.PointF[] points =
            {
                new System.Drawing.PointF(this.random.Next(rect.Width) / v,              this.random.Next(rect.Height) / v),
                new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
                new System.Drawing.PointF(this.random.Next(rect.Width) / v,              rect.Height - this.random.Next(rect.Height) / v),
                new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
            };
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0F, 0F);
            path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);

            // Draw the text.
            hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
                System.Drawing.Drawing2D.HatchStyle.LargeConfetti
                , System.Drawing.Color.LightGray
                , System.Drawing.Color.DarkGray);
            g.FillPath(hatchBrush, path);

            // Add some random noise.
            int m = System.Math.Max(rect.Width, rect.Height);

            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = this.random.Next(rect.Width);
                int y = this.random.Next(rect.Height);
                int w = this.random.Next(m / 50);
                int h = this.random.Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }

            // Clean up.
            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();

            // Set the image.
            this.image = bitmap;
        }
Exemplo n.º 8
0
        private byte[] GenerateCaptchaImage(string text, int width, int height)
        {
            Random random = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));

            System.Drawing.Font font;
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmp);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height);
            System.Drawing.Drawing2D.HatchBrush brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.White);
            graphics.FillRectangle(brush, rect);

            float emSize = rect.Height + 1;

            do {
                emSize--;
                font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, emSize, System.Drawing.FontStyle.Bold);
            } while (graphics.MeasureString(text, font).Width > rect.Width);

            System.Drawing.StringFormat format = new System.Drawing.StringFormat {
                Alignment = System.Drawing.StringAlignment.Center,
                LineAlignment = System.Drawing.StringAlignment.Center
            };

            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(text, font.FontFamily, (int)font.Style, 75f, rect, format);
            float num2 = 4f;
            System.Drawing.PointF[] destPoints = new System.Drawing.PointF[] { new System.Drawing.PointF(((float)random.Next(rect.Width)) / num2, ((float)random.Next(rect.Height)) / num2), new System.Drawing.PointF(rect.Width - (((float)random.Next(rect.Width)) / num2), ((float)random.Next(rect.Height)) / num2), new System.Drawing.PointF(((float)random.Next(rect.Width)) / num2, rect.Height - (((float)random.Next(rect.Height)) / num2)), new System.Drawing.PointF(rect.Width - (((float)random.Next(rect.Width)) / num2), rect.Height - (((float)random.Next(rect.Height)) / num2)) };
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0f, 0f);
            path.Warp(destPoints, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0f);
            brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent10, System.Drawing.Color.Black, System.Drawing.Color.SkyBlue);
            graphics.FillPath(brush, path);

            int num3 = Math.Max(rect.Width, rect.Height);
            for (int i = 0; i < ((int)(((float)(rect.Width * rect.Height)) / 30f)); i++) {
                int x = random.Next(rect.Width);
                int y = random.Next(rect.Height);
                int w = random.Next(num3 / 50);
                int h = random.Next(num3 / 50);
                graphics.FillEllipse(brush, x, y, w, h);
            }
            font.Dispose();
            brush.Dispose();
            graphics.Dispose();

            System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
            bmp.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            byte[] imageContent = new Byte[imageStream.Length];
            imageStream.Position = 0;
            imageStream.Read(imageContent, 0, (int)imageStream.Length);
            return imageContent;
        }