C#返回验证码图片内存流字符串

2022/3/11 7:14:45

本文主要是介绍C#返回验证码图片内存流字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.上代码:

方法:

        /// <summary>
        /// 生成随机验证码数字+字母
        /// </summary>
        /// <param name="codelen">验证码长度</param>
        /// <returns>返回验证码</returns>
        public static string MakeCode(int codelen)
        {
            if (codelen < 1)
            {
                return string.Empty;
            }
            int number;
            StringBuilder strCheckCode = new StringBuilder();
            Random random = new Random();
            for (int index = 0; index < codelen; index++)
            {
                number = random.Next();
                if (number % 2 == 0)
                {
                    strCheckCode.Append((char)('0' + (char)(number % 10)));//生成随机数字
                }
                else
                {
                    strCheckCode.Append((char)('A' + (char)(number % 26)));//生成随机字母
                }
            }
            return strCheckCode.ToString();
        }

        /// <summary>
        /// 根据验证码返回验证码图片
        /// </summary>
        /// <param name="CheckCode">验证码</param>
        /// <returns></returns>
        public string CheckCodeImage(string CheckCode)
        {
            if (string.IsNullOrEmpty(CheckCode))
            {
                return null;
            }
            Bitmap image = new Bitmap((int)Math.Ceiling((CheckCode.Length * 12.5)), 22);
            Graphics graphic = Graphics.FromImage(image);//创建一个验证码图片
            try
            {
                Random random = new Random();
                graphic.Clear(Color.White);
                int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
                for (int index = 0; index < 25; index++)
                {
                    x1 = random.Next(image.Width);
                    x2 = random.Next(image.Width);
                    y1 = random.Next(image.Height);
                    y2 = random.Next(image.Height);
                    graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));//Font设置字体,字号,字形
                //设置图形渐变色的起始颜色与终止颜色,渐变角度
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
                graphic.DrawString(CheckCode, font, brush, 2, 2);
                int X = 0; int Y = 0;
                //绘制图片的前景噪点
                for (int i = 0; i < 100; i++)
                {
                    X = random.Next(image.Width);
                    Y = random.Next(image.Height);
                    image.SetPixel(X, Y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                //将图片保存为stream流返回
                MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0,(int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            finally
            {
                graphic.Dispose();
                image.Dispose();
            }
        }

前端使用:

<img src='data:image/gif;base64, 加上返回的字符串/>

 

效果:

 

 

 

感谢:https://blog.csdn.net/weixin_42524279/article/details/87879624

https://www.cnblogs.com/dobiprogrammer/p/9722412.html

 



这篇关于C#返回验证码图片内存流字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程