Search code examples
asp.net-coredrawing

Using Drawing to create a graphic verification code does not display


I am developing an API tool for generating verification codes. I use the Drawing package. I have found some methods, but why can't I generate the graphic verification codes I need? Where am I doing wrong?

 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();
                    }
                }
            }
        }

Solution

  • I have a complete code for generating verification code using Bitmap, you can refer to the following:

    Controller:

    [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 to generate verification code:

     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;
            }
        }
    

    Result:

    enter image description here