• 【日常笔记】生成验证码图片


            public string MakeValidateCode()
            {
                char[] s = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
                string num = "";
                Random r = new Random();
                for (int i = 0; i < 4; i++)
                {
                    num += s[r.Next(0, s.Length)].ToString();
                }
                return num;
            }

    生成验证所需的字母和数字组合

            public byte[] CreateCheckCodeImage(string checkCode)
            {
                Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };
                int len = colors.Length;
    
                System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(checkCode.Length * 14.5d), 25);
                Graphics g = Graphics.FromImage(image);
                try
                {
                    //生成随机生成器
                    Random random = new Random();
                    //清空图片背景色
                    g.Clear(Color.White);
                    Font font = new System.Drawing.Font("Arial", 12, FontStyle.Bold);
                    System.Drawing.Brush brush = new SolidBrush(colors[random.Next(0, len)]);
                    SizeF size = g.MeasureString(checkCode, font);
                    g.DrawString(checkCode, font, brush, (image.Width - size.Width) / 2, (image.Height - size.Height) / 2);
    
                    Twist tw = new Twist();
                    //正弦扭曲 沿X轴
                    image = tw.TwistBitmap(image, false, 4, Math.PI);
                    //正弦扭曲 沿Y轴
                    image = tw.TwistBitmap(image, true, 2, Math.PI / 2);
    
                    g.Dispose();
    
                    g = Graphics.FromImage(image);
                    //画图片的背景噪音线
                    for (int i = 0; i < 2; 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);
                        g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
                    }
                    //画图片的边框线
                    g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
    
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                    return ms.ToArray();
                }
                finally
                {
                    g.Dispose();
                    image.Dispose();
                }
            }

    生成图片,并且转化为字节数据

  • 相关阅读:
    js,jQuery实现可关闭悬浮框广告特效,兼容(谷歌,火狐,Ie)
    各种选择框jQuery的选中方法
    表单校验demo
    两种方法实现城市级联菜单
    树形菜单
    匿名函数和鼠标移入移除事件
    多线程实例
    Lock锁
    Oracle语句
    Java NIO
  • 原文地址:https://www.cnblogs.com/weimingtian/p/9339706.html
Copyright © 2020-2023  润新知