生成验证码

来源:互联网 发布:ubuntu likewise open 编辑:程序博客网 时间:2024/06/06 17:54

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


 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();
原创粉丝点击