生成验证码

先生成随机的四位字母,每个字母在生成的图像中尺寸随机生成,有一定角度的旋转,颜色也有一定程度的变化


 var code = Request["code"];

            if (code == null)
                throw new HttpException(403, "need code");




            Response.ContentType = "image/jpeg";
            Response.BufferOutput = true;
            Response.Clear();


            using (var image = new Bitmap(640, 320))
            {
                image.SetResolution(72.0f, 72.0f);
                using (var graphics = Graphics.FromImage(image))
                {
                    WebColorConverter ww = new WebColorConverter();
                    graphics.Clear((Color)ww.ConvertFromString("#FAE264"));


                    Random random = new Random();
                    //画图片的背景噪音线
                    for (int i = 0; i < 12; i++)
                    {
                        int x1 = random.Next(image.Width);
                        int x2 = random.Next(image.Width);
                        int y1 = random.Next(image.Height);
                        int y2 = random.Next(image.Height);


                        graphics.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
                    }


                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                    {
                        using (var font = new Font("Arial", (float)(random.NextDouble()*100+100), FontStyle.Bold | FontStyle.Italic))
                        {
                            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                                new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Red, (float)(random.NextDouble()*20+30), true);
                            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
                            matrix.Rotate((float)(random.NextDouble()*20-10));
                            graphics.Transform = matrix;
                            graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(0, 1).ToUpper() : code.Substring(0, 1), font, brush, random.Next(30, 40), random.Next(35, 45));
                        }


                        using (var font = new Font("Arial", (float)(random.NextDouble() * 100 + 100), FontStyle.Bold | FontStyle.Italic))
                        {
                            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                                new Rectangle(0, 0, image.Width, image.Height), Color.Purple, Color.Pink, (float)(random.NextDouble() * 20 + 30), true);
                            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
                            matrix.Rotate((float)(random.NextDouble() * 20 - 10));
                            graphics.Transform = matrix;
                            graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(1, 1).ToUpper() : code.Substring(1, 1), font, brush, random.Next(172, 183), random.Next(35, 45));
                        }


                        using (var font = new Font("Arial", (float)(random.NextDouble() * 100 + 100), FontStyle.Bold | FontStyle.Italic))
                        {
                            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                                new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.White, (float)(random.NextDouble() * 20 + 30), true);
                            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
                            matrix.Rotate((float)(random.NextDouble() * 20 - 10));
                            graphics.Transform = matrix;
                            graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(2, 1).ToUpper() : code.Substring(2, 1), font, brush, random.Next(314, 324), random.Next(35, 45));
                        }


                        using (var font = new Font("Arial", (float)(random.NextDouble() * 100 + 100), FontStyle.Bold | FontStyle.Italic))
                        {
                            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                                new Rectangle(0, 0, image.Width, image.Height), Color.Silver, Color.Red, (float)(random.NextDouble() * 20 + 30), true);
                            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
                            matrix.Rotate((float)(random.NextDouble() * 20 - 10));
                            graphics.Transform = matrix;
                            graphics.DrawString(random.Next(0, 2) == 0 ? code.Substring(3, 1).ToUpper() : code.Substring(3, 1), font, brush, random.Next(456, 466), random.Next(35, 45));
                        }


                    }
                }
                var jpegParams = new EncoderParameters(1);
                jpegParams.Param[0] = new EncoderParameter(Encoder.Quality, 30L);
                image.Save(Response.OutputStream, ImageCodecInfo.GetImageDecoders().Single(decoder => decoder.FormatID == ImageFormat.Jpeg.Guid), jpegParams);
                Response.Flush();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值