Exemplo n.º 1
0
        /// <summary>获取验证码图片信息</summary>
        /// <returns>验证码图片信息</returns>
        public VerifyCodeImageInfo GetVerifyCodeImageInfo()
        {
            var vcii = new VerifyCodeImageInfo();

            vcii.Text        = this.GetVerifyCode(5);
            vcii.ImageWidth  = WebGet.GetInt("w", 200);
            vcii.ImageHeight = WebGet.GetInt("h", 80);
            var tc = WebGet.GetString("tc");

            if (tc.IsNullOrEmpty_())
            {
                vcii.TextColor       = Color.Empty;
                vcii.RandomTextColor = true;
            }
            else
            {
                vcii.TextColor       = ColorTranslator.FromHtml(tc);
                vcii.RandomTextColor = WebGet.GetString("r").ToBool_();
            }
            var bc = WebGet.GetString("bc");

            vcii.BackgroundColor = bc.IsNullOrEmpty_() ? Color.White : ColorTranslator.FromHtml(bc);
            vcii.ImageFormat     = ImageFormat.Png;
            this.CreateVerifyCodeImage(vcii);

            return(vcii);
        }
Exemplo n.º 2
0
        /// <summary>创建验证码图片</summary>
        /// <param name="verifyCodeImageInfo">验证码图片信息</param>
        public void CreateVerifyCodeImage(VerifyCodeImageInfo verifyCodeImageInfo)
        {
            int textLength = verifyCodeImageInfo.Text.Length;
            if(textLength == 0 || verifyCodeImageInfo.ImageWidth == 0 || verifyCodeImageInfo.ImageHeight == 0) return;

            using(Bitmap img = new Bitmap(verifyCodeImageInfo.ImageWidth, verifyCodeImageInfo.ImageHeight, PixelFormat.Format32bppArgb))
            {
                using(GraphicsPath p = new GraphicsPath())
                {
                    using(Graphics g = Graphics.FromImage(img))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.Clear(verifyCodeImageInfo.BackgroundColor);

                        int charSize = (int)(Math.Min(verifyCodeImageInfo.ImageHeight, (int)(verifyCodeImageInfo.ImageWidth / textLength)) * 0.8);
                        PointF charPoint = new PointF();
                        int halfCharSize = (int)(charSize * 0.5f);
                        int paddingHeight = (int)(verifyCodeImageInfo.ImageHeight * 0.8) - charSize;

                        using(Matrix matrix = new Matrix())
                        {
                            using(Pen pen = new Pen(Color.Empty))
                            {
                                using(StringFormat format = new StringFormat())
                                {
                                    format.Alignment = StringAlignment.Near;
                                    format.LineAlignment = StringAlignment.Near;
                                    FontFamily f;
                                    for(int i = 0; i < textLength; i++)
                                    {
                                        charPoint.X = (float)(i * charSize + Utils.Rand((int)(charSize * 0.2f), (int)(charSize * 0.8f)));
                                        charPoint.Y = (float)Utils.Rand(2, paddingHeight);

                                        p.Reset();
                                        float fs = charSize * Utils.Rand(80, 120) * 0.01f;
                                        f = GetFontFamily(verifyCodeImageInfo.Fonts);
                                        p.AddString(verifyCodeImageInfo.Text[i].ToString(), f, 0, fs, new Point(0, 0), format);

                                        matrix.Reset();
                                        matrix.RotateAt((float)Utils.Rand(-60, 60), new PointF(charPoint.X + halfCharSize, charPoint.Y + halfCharSize));
                                        matrix.Translate(charPoint.X, charPoint.Y);
                                        p.Transform(matrix);

                                        pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                        pen.Width = Utils.Rand(1, 2);
                                        g.DrawPath(pen, p);
                                        g.DrawBezier(pen,
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight));
                                    }
                                }
                                pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                pen.Width = Utils.Rand(1, 2);
                                g.DrawBezier(pen,
                                    Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                    0,
                                    Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                    Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                    Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                    Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                    Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                    verifyCodeImageInfo.ImageHeight);
                                pen.Width = Utils.Rand(1, 2);
                                pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                g.DrawBezier(pen,
                                    0,
                                    Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                    Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                    Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                    Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                    Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                    verifyCodeImageInfo.ImageWidth,
                                    Utils.Rand(verifyCodeImageInfo.ImageHeight));
                            }
                        }
                    }
                }
                verifyCodeImageInfo.ImageData = img.Clone() as Image;
            }
        }
Exemplo n.º 3
0
        /// <summary>获取验证码图片信息</summary>
        /// <returns>验证码图片信息</returns>
        public VerifyCodeImageInfo GetVerifyCodeImageInfo()
        {
            var vcii = new VerifyCodeImageInfo();
            vcii.Text = this.GetVerifyCode(5);
            vcii.ImageWidth = WebGet.GetInt("w", 200);
            vcii.ImageHeight = WebGet.GetInt("h", 80);
            var tc = WebGet.GetString("tc");
            if(tc.IsNullOrEmpty_())
            {
                vcii.TextColor = Color.Empty;
                vcii.RandomTextColor = true;
            }
            else
            {
                vcii.TextColor = ColorTranslator.FromHtml(tc);
                vcii.RandomTextColor = WebGet.GetString("r").ToBool_();
            }
            var bc = WebGet.GetString("bc");
            vcii.BackgroundColor = bc.IsNullOrEmpty_() ? Color.White : ColorTranslator.FromHtml(bc);
            vcii.ImageFormat = ImageFormat.Png;
            this.CreateVerifyCodeImage(vcii);

            return vcii;
        }
Exemplo n.º 4
0
        /// <summary>创建验证码图片</summary>
        /// <param name="verifyCodeImageInfo">验证码图片信息</param>
        public void CreateVerifyCodeImage(VerifyCodeImageInfo verifyCodeImageInfo)
        {
            int textLength = verifyCodeImageInfo.Text.Length;
            if(textLength == 0 || verifyCodeImageInfo.ImageWidth == 0 || verifyCodeImageInfo.ImageHeight == 0) return;

            using(var img = new Bitmap(verifyCodeImageInfo.ImageWidth, verifyCodeImageInfo.ImageHeight, PixelFormat.Format32bppArgb))
            {
                using(var g = Graphics.FromImage(img))
                {
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.Clear(verifyCodeImageInfo.BackgroundColor);

                    int charSize = (int)(Math.Min(verifyCodeImageInfo.ImageHeight, (int)(verifyCodeImageInfo.ImageWidth / textLength)) * 0.8);
                    PointF charPoint = new PointF();
                    int halfCharSize = (int)(charSize * 0.5f);
                    int paddingHeight = (int)(verifyCodeImageInfo.ImageHeight * 0.6) - charSize;

                    using(var matrix = new Matrix())
                    {
                        using(var pen = new Pen(Color.Empty))
                        {
                            pen.Width = 1f;
                            using(var b = new SolidBrush(verifyCodeImageInfo.TextColor))
                            {
                                using(var format = new StringFormat())
                                {
                                    format.Alignment = StringAlignment.Near;
                                    format.LineAlignment = StringAlignment.Near;
                                    Font f;
                                    for(int i = 0; i < textLength; i++)
                                    {
                                        // 位置
                                        charPoint.X += i == 0 ? charSize * 0.2f : charSize * 1.1f;// (float)(i * charSize);
                                        charPoint.Y = (float)Utils.Rand(4, paddingHeight);
                                        // 旋转
                                        matrix.Reset();
                                        matrix.RotateAt((float)Utils.Rand(-20, 20),
                                            new PointF(charPoint.X + halfCharSize, charPoint.Y + halfCharSize));
                                        //matrix.Translate(charPoint.X, charPoint.Y);
                                        g.Transform = matrix;

                                        b.Color = pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);

                                        // 字符大小
                                        float fs = charSize * Utils.Rand(80, 120) * 0.01f;
                                        f = new Font(GetFontFamily(verifyCodeImageInfo.Fonts), fs);

                                        g.DrawString(verifyCodeImageInfo.Text[i].ToString(), f, b, charPoint, format);
                                        f.Dispose();
                                        g.ResetTransform();

                                        // 使用字符相同颜色随机绘制曲线
                                        //pen.Width = Utils.Rand(1, 2);
                                        g.DrawBezier(pen,
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                            Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                            Utils.Rand(verifyCodeImageInfo.ImageHeight));

                                    }

                                    // 绘制随机纵向曲线
                                    pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                    //pen.Width = Utils.Rand(1, 2);
                                    g.DrawBezier(pen,
                                        Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                        0,
                                        Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                        Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                        Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                        Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                        Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                        verifyCodeImageInfo.ImageHeight);
                                    // 绘制随机横向曲线
                                    //pen.Width = Utils.Rand(1, 2);
                                    pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                    g.DrawBezier(pen,
                                        0,
                                        Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                        Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                        Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                        Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                        Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                        verifyCodeImageInfo.ImageWidth,
                                        Utils.Rand(verifyCodeImageInfo.ImageHeight));

                                }
                            }
                        }
                    }
                }
                verifyCodeImageInfo.ImageData = img.Clone() as Image;
            }
        }
Exemplo n.º 5
0
        /// <summary>创建验证码图片</summary>
        /// <param name="verifyCodeImageInfo">验证码图片信息</param>
        public void CreateVerifyCodeImage(VerifyCodeImageInfo verifyCodeImageInfo)
        {
            int textLength = verifyCodeImageInfo.Text.Length;

            if (textLength == 0 || verifyCodeImageInfo.ImageWidth == 0 || verifyCodeImageInfo.ImageHeight == 0)
            {
                return;
            }

            using (Bitmap img = new Bitmap(verifyCodeImageInfo.ImageWidth, verifyCodeImageInfo.ImageHeight, PixelFormat.Format32bppArgb))
            {
                using (GraphicsPath p = new GraphicsPath())
                {
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.Clear(verifyCodeImageInfo.BackgroundColor);

                        int    charSize      = (int)(Math.Min(verifyCodeImageInfo.ImageHeight, (int)(verifyCodeImageInfo.ImageWidth / textLength)) * 0.8);
                        PointF charPoint     = new PointF();
                        int    halfCharSize  = (int)(charSize * 0.5f);
                        int    paddingHeight = (int)(verifyCodeImageInfo.ImageHeight * 0.8) - charSize;

                        using (Matrix matrix = new Matrix())
                        {
                            using (Pen pen = new Pen(Color.Empty))
                            {
                                using (StringFormat format = new StringFormat())
                                {
                                    format.Alignment     = StringAlignment.Near;
                                    format.LineAlignment = StringAlignment.Near;
                                    FontFamily f;
                                    for (int i = 0; i < textLength; i++)
                                    {
                                        charPoint.X = (float)(i * charSize + Utils.Rand((int)(charSize * 0.2f), (int)(charSize * 0.8f)));
                                        charPoint.Y = (float)Utils.Rand(2, paddingHeight);

                                        p.Reset();
                                        float fs = charSize * Utils.Rand(80, 120) * 0.01f;
                                        f = GetFontFamily(verifyCodeImageInfo.Fonts);
                                        p.AddString(verifyCodeImageInfo.Text[i].ToString(), f, 0, fs, new Point(0, 0), format);

                                        matrix.Reset();
                                        matrix.RotateAt((float)Utils.Rand(-60, 60), new PointF(charPoint.X + halfCharSize, charPoint.Y + halfCharSize));
                                        matrix.Translate(charPoint.X, charPoint.Y);
                                        p.Transform(matrix);

                                        pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                        pen.Width = Utils.Rand(1, 2);
                                        g.DrawPath(pen, p);
                                        g.DrawBezier(pen,
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight));
                                    }
                                }
                                pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                pen.Width = Utils.Rand(1, 2);
                                g.DrawBezier(pen,
                                             Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                             0,
                                             Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                             Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                             Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                             Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                             Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                             verifyCodeImageInfo.ImageHeight);
                                pen.Width = Utils.Rand(1, 2);
                                pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                g.DrawBezier(pen,
                                             0,
                                             Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                             Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                             Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                             Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                             Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                             verifyCodeImageInfo.ImageWidth,
                                             Utils.Rand(verifyCodeImageInfo.ImageHeight));
                            }
                        }
                    }
                }
                verifyCodeImageInfo.ImageData = img.Clone() as Image;
            }
        }
Exemplo n.º 6
0
        /// <summary>创建验证码图片</summary>
        /// <param name="verifyCodeImageInfo">验证码图片信息</param>
        public void CreateVerifyCodeImage(VerifyCodeImageInfo verifyCodeImageInfo)
        {
            int textLength = verifyCodeImageInfo.Text.Length;

            if (textLength == 0 || verifyCodeImageInfo.ImageWidth == 0 || verifyCodeImageInfo.ImageHeight == 0)
            {
                return;
            }

            using (var img = new Bitmap(verifyCodeImageInfo.ImageWidth, verifyCodeImageInfo.ImageHeight, PixelFormat.Format32bppArgb))
            {
                using (var g = Graphics.FromImage(img))
                {
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.Clear(verifyCodeImageInfo.BackgroundColor);

                    int    charSize      = (int)(Math.Min(verifyCodeImageInfo.ImageHeight, (int)(verifyCodeImageInfo.ImageWidth / textLength)) * 0.8);
                    PointF charPoint     = new PointF();
                    int    halfCharSize  = (int)(charSize * 0.5f);
                    int    paddingHeight = (int)(verifyCodeImageInfo.ImageHeight * 0.6) - charSize;

                    using (var matrix = new Matrix())
                    {
                        using (var pen = new Pen(Color.Empty))
                        {
                            pen.Width = 1f;
                            using (var b = new SolidBrush(verifyCodeImageInfo.TextColor))
                            {
                                using (var format = new StringFormat())
                                {
                                    format.Alignment     = StringAlignment.Near;
                                    format.LineAlignment = StringAlignment.Near;
                                    Font f;
                                    for (int i = 0; i < textLength; i++)
                                    {
                                        // 位置
                                        charPoint.X += i == 0 ? charSize * 0.2f : charSize * 1.1f;                                        // (float)(i * charSize);
                                        charPoint.Y  = (float)Utils.Rand(4, paddingHeight);
                                        // 旋转
                                        matrix.Reset();
                                        matrix.RotateAt((float)Utils.Rand(-20, 20),
                                                        new PointF(charPoint.X + halfCharSize, charPoint.Y + halfCharSize));
                                        //matrix.Translate(charPoint.X, charPoint.Y);
                                        g.Transform = matrix;

                                        b.Color = pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);

                                        // 字符大小
                                        float fs = charSize * Utils.Rand(80, 120) * 0.01f;
                                        f = new Font(GetFontFamily(verifyCodeImageInfo.Fonts), fs);

                                        g.DrawString(verifyCodeImageInfo.Text[i].ToString(), f, b, charPoint, format);
                                        f.Dispose();
                                        g.ResetTransform();

                                        // 使用字符相同颜色随机绘制曲线
                                        //pen.Width = Utils.Rand(1, 2);
                                        g.DrawBezier(pen,
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                     Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                     Utils.Rand(verifyCodeImageInfo.ImageHeight));
                                    }

                                    // 绘制随机纵向曲线
                                    pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                    //pen.Width = Utils.Rand(1, 2);
                                    g.DrawBezier(pen,
                                                 Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                 0,
                                                 Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                 Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                 Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                 Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                 Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                 verifyCodeImageInfo.ImageHeight);
                                    // 绘制随机横向曲线
                                    //pen.Width = Utils.Rand(1, 2);
                                    pen.Color = GetTextColor(verifyCodeImageInfo.RandomTextColor, verifyCodeImageInfo.TextColor);
                                    g.DrawBezier(pen,
                                                 0,
                                                 Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                 Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                 Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                 Utils.Rand(verifyCodeImageInfo.ImageWidth),
                                                 Utils.Rand(verifyCodeImageInfo.ImageHeight),
                                                 verifyCodeImageInfo.ImageWidth,
                                                 Utils.Rand(verifyCodeImageInfo.ImageHeight));
                                }
                            }
                        }
                    }
                }
                verifyCodeImageInfo.ImageData = img.Clone() as Image;
            }
        }