使用绘图创建图形验证码不显示

Using Drawing to create a graphic verification code does not display

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

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

我有完整的使用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;
        }
    }

结果: