阿里旺旺验证码不显示:使用绘图创建图形验证码不显示

关于阿里旺旺验证码不显示的问题,在asp graphics中经常遇到, 我正在开发一个生成验证码的 API 工具。我使用 Drawing 包。我已经找到了一些方法,但是为什么我不能生成我需要的图形验证码?我在哪里做错了?

我正在开发一个生成验证码的 API 工具。我使用 Drawing 包。我已经找到了一些方法,但是为什么我不能生成我需要的图形验证码?我在哪里做错了?

 public void Output(HttpResponse objHttpResponse)
        {
            using (Bitmap bitmap = this.GetImage())
            {
                if (bitmap != null)
                {
                    using (MemoryStream ms= new MemoryStream())
                    {
                        bitmap .Save(ms, ImageFormat.Jpeg);
 
                        HttpContext.Current.Response.ClearContent();
                        HttpContext.Current.Response.ContentType = "image/Jpeg";
                        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.End();
                    }
                }
            }
        }
0

我有一个使用 Bitmap 生成验证码的完整代码,您可以参考以下内容:

控制器:

[ApiController]
public class CaptchaController : Controller
{
    [Route("get_captcha")]
    public Object VerifyCode()
    {
        string code = "";
        Bitmap bitmap = Captcha.CreateCaptcha(out code);
        MemoryStream stream = new MemoryStream();
        bitmap.Save(stream, ImageFormat.Gif);
        return File(stream.ToArray(), "image/gif");
    }
}

生成验证码的 API:

 public class Captcha
    {
        public  static Bitmap CreateCaptcha(out string code)
        {
            //Create a Bitmap object and draw
            Bitmap bitmap = new Bitmap(200, 60);
            Graphics graph = Graphics.FromImage(bitmap);
            graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
            Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            string letters = "0123456789";
            StringBuilder sb = new StringBuilder();
            //Add random 4 numbers
            for (int x = 0; x < 4; x++)
            {
                string letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
                sb.Append(letter);
                graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
            }
            code = sb.ToString();
            //Confuse the background
            Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
            for (int x = 0; x < 6; x++)
                graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
            return bitmap;
        }
    }

结果:

enter image description here

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(656)
Perplexed:由SVGviewBox 宽度 高度等困惑
上一篇
营销程序:Web营销活动归因于应用程序安装
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(79条)