效果预览:

1.随机码和图片流生成

public class ValidateCode
{
/// <summary>
/// 產生圖形驗證碼。
/// </summary>
/// <param name="Code">傳出驗證碼。</param>
/// <param name="CodeLength">驗證碼字元數。</param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="FontSize"></param>
/// <returns></returns>
public static byte[] CreateValidateGraphic(out String Code, int CodeLength, int Width, int Height, int FontSize)
{
String sCode = String.Empty;
//顏色列表,用於驗證碼、噪線、噪點
Color[] oColors ={
System.Drawing.Color.Black,
System.Drawing.Color.Red,
System.Drawing.Color.Blue,
System.Drawing.Color.Green,
System.Drawing.Color.Orange,
System.Drawing.Color.Brown,
System.Drawing.Color.Brown,
System.Drawing.Color.DarkBlue
};
//字體列表,用於驗證碼
string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//驗證碼的字元集,去掉了一些容易混淆的字元
char[] oCharacter = {
'2','3','4','5','6','8','9',
'A','B','C','D','E','F','G','H','J','K', 'L','M','N','P','R','S','T','W','X','Y'
};
Random oRnd = new Random();
Bitmap oBmp = null;
Graphics oGraphics = null;
int N1 = 0;
System.Drawing.Point oPoint1 = default(System.Drawing.Point);
System.Drawing.Point oPoint2 = default(System.Drawing.Point);
string sFontName = null;
Font oFont = null;
Color oColor = default(Color); //生成驗證碼字串
for (N1 = 0; N1 <= CodeLength - 1; N1++)
{
sCode += oCharacter[oRnd.Next(oCharacter.Length)];
} oBmp = new Bitmap(Width, Height);
oGraphics = Graphics.FromImage(oBmp);
oGraphics.Clear(System.Drawing.Color.White);
try
{
for (N1 = 0; N1 <= 4; N1++)
{
//畫噪線
oPoint1.X = oRnd.Next(Width);
oPoint1.Y = oRnd.Next(Height);
oPoint2.X = oRnd.Next(Width);
oPoint2.Y = oRnd.Next(Height);
oColor = oColors[oRnd.Next(oColors.Length)];
oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
} float spaceWith = 0, dotX = 0, dotY = 0;
if (CodeLength != 0)
{
spaceWith = (Width - FontSize * CodeLength - 10) / CodeLength;
} for (N1 = 0; N1 <= sCode.Length - 1; N1++)
{
//畫驗證碼字串
sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
oFont = new Font(sFontName, FontSize, FontStyle.Italic);
oColor = oColors[oRnd.Next(oColors.Length)]; dotY = (Height - oFont.Height) / 2 + 2;//中心下移2像素
dotX = Convert.ToSingle(N1) * FontSize + (N1 + 1) * spaceWith; oGraphics.DrawString(sCode[N1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
} for (int i = 0; i <= 30; i++)
{
//畫噪點
int x = oRnd.Next(oBmp.Width);
int y = oRnd.Next(oBmp.Height);
Color clr = oColors[oRnd.Next(oColors.Length)];
oBmp.SetPixel(x, y, clr);
} Code = sCode;
//保存图片数据
MemoryStream stream = new MemoryStream();
oBmp.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
oGraphics.Dispose();
}
}
}

图片流以图片的形式响应到页面

public class ValidateCodeController : Controller
{
public ActionResult GetImg()
{
int width = ConverterHelper.ObjToInt(Request.Params["width"], 100);
int height = ConverterHelper.ObjToInt(Request.Params["height"], 40);
int fontsize = ConverterHelper.ObjToInt(Request.Params["fontsize"], 20);
string code = string.Empty;
byte[] bytes = ValidateCode.CreateValidateGraphic(out code, 4, width, height, fontsize);
SessionHelper.SetValiCode(code);
return File(bytes, @"image/jpeg");
} }

页面显示及刷新(img+js)

<img id="GL_StandardCode"  style="cursor: pointer;" src="@Url.Action("GetImg", "ValidateCode")?t=@DateTime.Now.Ticks" title="看不清,点击换一张" />

            $("#GL_StandardCode").click(function () {
var newSrc = "@Url.Action("GetImg", "ValidateCode")" + "?t=" + (new Date()).getTime();
this.src=newSrc;
return false;
});

登录时判断SESSION值

 string pCode = Request.Params["GL_CodeInput"];
string sCode = SessionHelper.GetValiCode();
if (string.IsNullOrEmpty(pCode))
{
resultMsg = "请输入验证码";
}
else if (string.IsNullOrEmpty(sCode))
{
resultMsg = "验证码过期";
}
else if (pCode.ToLower() != sCode.ToLower())
{
resultMsg = "验证码不正确";
}

最新文章

  1. 打不死的redis集群
  2. POJ 3669 Meteor Shower【BFS】
  3. word20161204
  4. 微博转发关系采集,可拓展关键字采集,评论采集(Java版)
  5. 无线电源传输 Wireless Power Consortium (WPC) Communication
  6. 浅析I/O模型及其设计模式
  7. Redis高级实践之————Redis短连接性能优化
  8. DBCC Check
  9. CardView官方教程
  10. 3rd day
  11. SQL Serve数据库排序空值null始终前置的方法
  12. vim使用(三):.viminfo和.vimrc
  13. 360的IM可能会是什么样?
  14. springMVC获取数据--注意post方法会出现中文乱码问题
  15. [R] Lexical &amp; Dynamic Scoping / Execution &amp; Calling environments / Closures
  16. 初用Ajax
  17. WM_COMMAND消息
  18. python3学习笔记五(列表2)
  19. Python3 图像边界识别
  20. C#-MVC开发微信应用(6)--用户分组信息管理

热门文章

  1. Spring Security验证,提示正确的信息
  2. zabbix实现自定义自动发现的流程
  3. Mesos Marathon能做什么?理念是什么?(转)
  4. SpringBoot WebSocket 消息交互
  5. SpringIOC基础知识总结
  6. [web安全原理分析]-XEE漏洞入门
  7. 一次看完28个关于ES的性能调优技巧,很赞,值得收藏!
  8. MathType单边大括号的编辑技巧你知道吗?
  9. python 如何跳过异常继续执行
  10. JAVA 中的Optional (臭名昭著的空指针异常(NullPointerException))