html验证码360问答,验证码源代码实现 c#

满意答案

00e27ab806e4881f8254fe7ae8741834.png

你好9898ok

2013.06.05

00e27ab806e4881f8254fe7ae8741834.png

采纳率:44%    等级:13

已帮助:15990人

using System; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; /// /// Summary description for yanzhengma /// public class Yanzhengma { private string GetRandom() { int number; char charCode; string strCode = string.Empty; System.Random random = new System.Random(); for (int i = 1; i <= 5; i++) { number = random.Next(); if (number % 2 == 0) { charCode = (char)('0' + (char)(number % 10)); } else { charCode = (char)('A' + (char)(number % 26)); } strCode += charCode.ToString(); } // System.Web.HttpContext.Current.Response.Cookies HttpContext.Current.Response.Cookies.Add(new HttpCookie("codebycookies", strCode)); return strCode; } public void DrawRandom() { string strCode = GetRandom(); Bitmap image = new Bitmap(100, 30); Graphics huatu = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 huatu.Clear(Color.White); //画图片的背景噪音线 int x1, x2, y1, y2; for (int i = 0; i < 10; i++) { x1 = random.Next(image.Width); x2 = random.Next(image.Width); y1 = random.Next(image.Height); y2 = random.Next(image.Height); huatu.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 20, (FontStyle.Bold | System.Drawing.FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); huatu.DrawString(strCode, font, brush, 2, 2); //画图片的前景噪音点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 huatu.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width, image.Height); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.BinaryWrite(ms.ToArray()); } finally { huatu.Dispose(); image.Dispose(); } } }

01分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值