Home  >  Article  >  Backend Development  >  ASP.NET code tutorial for completing graphic verification code

ASP.NET code tutorial for completing graphic verification code

Y2J
Y2JOriginal
2017-05-16 10:10:391850browse

This article introduces you to the implementation method of asp.net mid-picture graphic verification code. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it.

Share this article with everyone A piece of asp.net code implements the function of drawing a graphical verification code. The code is simple and easy to understand. The specific code is as follows:

context.Response.ContentType = "image/jpeg";
   //生成随机的中文验证码
   string yzm = "人口手大小多少上中下男女天地会反清复明杨中科小宝双儿命名空间语现在明天来多个的我山东河北南固安北京南昌东海西安是沙河高教园学"
    + "木禾上下土个八入大天人火文六七儿九无口日中了子门月不开四五目耳头米见白田电也长山出飞马鸟云公车牛羊小少巾牙尺毛又心手水广升足"
    + "走方半巴业本平书自已东西回片皮生里果几用鱼今正雨两瓜衣来年左右万百丁齐冬说友话春朋高你绿们花红草爷亲节的岁行古处声知多忙洗真认父扫"
    + "母爸写全完关家看笑着兴画会妈合奶放午收女气太早去亮和李语秀千香听远唱定连向以更后意主总先起干明赶净同专工才级队蚂蚁前房空网诗黄林闭"
    + "童立是我朵叶美机她过他时送让吗往吧得虫很河借姐呢呀哪谁凉怕量跟最园脸因阳为光可法石找办许别那到都吓叫再做象点像照沙海桥军竹苗井面乡"
    + "忘想念王这从进边道贝男原爱虾跑吹乐地老快师短淡对热冷情拉活把种给吃练学习非苦常问伴间共伙汽分要没孩位选北湖南秋江只帮星请雪就球跳玩"
    + "桃树刚兰座各带坐急名发成动晚新有么在变什条";
   Random r = new Random();
   string validCode = "";
   for (int i = 0; i < 4; i++)
   {
    int number = r.Next(0, yzm.Length);
    validCode += yzm[number];
   }
   //画图
   using(Bitmap b = new Bitmap(150, 30))
   {
    using (Graphics g = Graphics.FromImage(b))
    using(Font f = new Font("宋体", 20))
    {
     g.Clear(Color.Yellow);//清除整个绘画面并填充指定背景色
     g.DrawString(validCode, f, Brushes.Red, new PointF(0, 0));//开始绘制
     //前景点
     for (int i = 0; i < 100; i++)
     {
      int x = r.Next(0, 150);
      int y = r.Next(0, 30);
      b.SetPixel(x, y, Color.Red);
     }
     //后景线
     for (int i = 0; i < 100; i++)
     {
      int x = r.Next(0, 150);
      int y = r.Next(0, 30);
      g.DrawLine(Pens.Red, x, y, x + 1, y + 1);
     }
    }
    b.Save(context.Response.OutputStream, ImageFormat.Jpeg);//保存到输出流中
   }

Reference:

A collection of methods for generating verification codes in asp.net (1 )

Asp.net developed webform image watermark and image verification code implementation method

[Related recommendations]

1. Special recommendation"php Programmer Toolbox" V0.1 version download

2. ASP free video tutorial

3. ASP Reference Manual

The above is the detailed content of ASP.NET code tutorial for completing graphic verification code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn