1、复制下列代码,拷贝到控制器中。
#region 生成验证码图片
// [OutputCache(Location = OutputCacheLocation.None, Duration = 0, NoStore = false)]
public ActionResult SecurityCode()
{
string oldcode = Session["SecurityCode"] as string;
string code = CreateRandomCode(5);
Session["SecurityCode"] = code;
return File(CreateValidateGraphic(code), "image/Jpeg");
}
private byte[] CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 12);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.White);
g.Clear(Color.Blue);
g.DrawString(checkCode, f, b, 3, 3)
C# MVC 验证码
最新推荐文章于 2023-10-04 05:17:52 发布