C#中生成随机验证码(摘抄)

None.gif public   void  MakeCheckCode( string  checkCodeCookieName,  int  len)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//这儿定义你期待产生的随机字母
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] arrLetter = new string[] dot.gif
InBlock.gif                                                   
"A""B""C""D""E""F""G""H""J""K""L""M""N""P""Q""R"
InBlock.gif                                                   
"S""T""U""V""W""X""Y""Z""2""3""4""5""6""7""8""9"
ExpandedSubBlockEnd.gif                                               }
;
InBlock.gif            
//随机颜色
ExpandedSubBlockStart.gifContractedSubBlock.gif
            Color[] arrColor = new Color[] dot.gif{ Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Green, Color.Brown, Color.DarkCyan, Color.Blue };
InBlock.gif            
//随机字体
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] arrFont = new string[] dot.gif"Arial""Verdana" };
InBlock.gif
InBlock.gif            Random romTemp 
= new Random();
InBlock.gif            
string strCheckCode = "";
InBlock.gif            
//产生随机字符串
InBlock.gif
            for (int i = 1; i <= len; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strCheckCode 
= strCheckCode + arrLetter[romTemp.Next(arrLetter.Length)];
ExpandedSubBlockEnd.gif            }

InBlock.gif            strCheckCode 
= strCheckCode.ToUpper();
InBlock.gif            
//将值写入cook
InBlock.gif
            HttpCookie cookTemp = new HttpCookie(checkCodeCookieName);
InBlock.gif            cookTemp.Values.Add(
"CheckCode", strCheckCode);
InBlock.gif            
this.Context.Response.Cookies.Add(cookTemp);
InBlock.gif            
int intWidth = strCheckCode.Length * 13//计算图片需要的宽度
InBlock.gif
            Bitmap bitmap1 = new Bitmap(intWidth,0x16);
InBlock.gif            
//绘图
InBlock.gif
            Graphics graphics1 = Graphics.FromImage(bitmap1);
InBlock.gif            graphics1.Clear(Color.White);
InBlock.gif            
for (int i = 0; i < 5; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//随机字母宽度
InBlock.gif
                int intLetterWidth = romTemp.Next(bitmap1.Width);
InBlock.gif                
//随机字母高度
InBlock.gif
                int intLetterHeight = romTemp.Next(bitmap1.Height);
InBlock.gif                graphics1.DrawRectangle(
new Pen(Color.DarkGray, 0f), intLetterWidth, intLetterHeight, 11);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
for (int i = 0; i < strCheckCode.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int num7 = romTemp.Next(arrColor.Length);
InBlock.gif                
int num8 = romTemp.Next(arrFont.Length);
InBlock.gif                Font font1 
= new Font(arrFont[num8], 10f, FontStyle.Bold);
InBlock.gif                Brush brush1 
= new SolidBrush(arrColor[num7]);
InBlock.gif                graphics1.DrawString(strCheckCode.Substring(i, 
1), font1, brush1, (float) (3 + (i * 11)), 3f);
ExpandedSubBlockEnd.gif            }

InBlock.gif            graphics1.DrawRectangle(
new Pen(Color.LightGray, 0f), 00, bitmap1.Width - 1, bitmap1.Height - 1);
InBlock.gif            graphics1.Dispose();
InBlock.gif            System.IO.MemoryStream stream1 
= new System.IO.MemoryStream();
InBlock.gif            bitmap1.Save(stream1, System.Drawing.Imaging.ImageFormat.Gif);
InBlock.gif            
this.Context.Response.ClearContent();
InBlock.gif            
this.Context.Response.ContentType = "image/Gif";
InBlock.gif            
this.Context.Response.BinaryWrite(stream1.ToArray());
InBlock.gif            bitmap1.Dispose();
InBlock.gif            graphics1.Dispose();
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/sangrei/archive/2007/01/28/632449.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值