using (Bitmap bitmap=new Bitmap(100,40))
{
using
(Graphics g=Graphics.FromImage(bitmap))
{
//生成验证码
string s = code();
g.DrawString(s, new
Font("黑体", 20), Brushes.Red, 0, 0);
bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
//改变bitmap的颜色 循环遍历
for (int j = 0; j < 100; j++)
{
for (int i = 0; i < 40; i++)
{
bitmap.SetPixel(j, i, Color.White);
}
}
//生成随机数的方法
private string code()
{
string s = "";
Random
rd = new Random();
for (int i = 0; i < 4; i++)
{
int a = rd.Next(0, 10);
s += a.ToString();
}
return s;
}
前台调用以及javascript
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta
http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
window.onload =
function () {
document.getElementById("img").onclick = function
() {
this.src = ‘huatu.ashx‘;
}
}
</script>
</head>
<body>
<img
src="huatu.ashx" alt="验证码" id="img" title="点击更换" style="cursor:pointer"
/>
</body>
</html>
html页内利用jQuery控制验证码的大小
<script src="jquery-1.8.3.js"></script>
<script>
$(function () {
$(‘#img‘).mouseover(function () {
$(this).css({ "width":
‘120px‘, "height": "60px" });
}).mouseout(function () {
$(this).css({ "width": ‘‘, "height": "" })
});
})
</script>
一般处理程序结合gdi生成简单验证码,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/lierjie/p/3747851.html