Implementation code of drawing graphic verification code in ASP. NET

  • 2021-09-11 19:52:27
  • OfStack

This article shares with you a section of asp. net code to realize the function of drawing graphic verification code. The code is simple and easy to understand. The specific code is as follows:


context.Response.ContentType = "image/jpeg";
   // Generate random Chinese verification code 
   string yzm = " What is the size of the population? The upper, middle and lower men's and women's heaven and earth meeting is anti-Qing and re-Ming Yang Zhongke Xiaobao Shuang'er namespace language. Now I will come to many places tomorrow. Shandong Hebei Nangu'an Beijing Nanchang Donghai Xi'an is Shahe Higher Education Park "
    + " Muhe goes up and down the soil 8 Into the Great Heaven and Man Huo Wen 67 Children 9 In the day of no mouth, the door of the child will not open 45 Ears, ears, head, rice, white fields, electricity, flying horses, birds, clouds, buses, cattle, sheep, small towels, teeth, feet, hair, heart, hands, water and feet "
    + " Go to the square, half-baye, this flat book, go back to your own things, go back to the skin, live in the fruit, use fish, rain, two melons and clothes, and talk about friends in the coming year or so. Spring friends are high, you are green, flowers, red grass, grandfather, pro-festival, old travel, ancient place sound, know how busy to wash, really recognize your father and sweep "
    + " Mom and Dad wrote it all, closed the family, looked at it with a smile, and started painting. Mom, milk, noon, women's spirit, too early to light up and Li Yuxiu Qianxiang listened to the distant singing, and even went to the future. The master always started first, dry and clean up the same specialized workers and talents. Ant front room empty net poem Huang Lin closed "
    + " Tong Li is my leaf beauty machine. When she passed him, did she give way? Go to the insects. I borrow my sister. Who is cool and afraid of measuring the most garden face? Because the sun is the light, you can find a stone to do it. Don't be scared. Do it again like a point like a photo of Shahai Bridge Military Bamboo Miao Well Township "
    + " Forget to miss Wang. This is from entering the border road. Bei Nan originally loved shrimp running and playing music. The old fast teacher was short and light. He pulled the heat and cold. He gave the seeds to eat and practice. Learning is not bitter. He often asked his companions to partner with each other. If there is no child position, choose the Qiujiang River in North Hunan Province, only help the stars ask for snow, and then jump and play with the ball "
    + " Peach tree just orchid seat each takes the urgent name, makes it move late, is it changing? ";
   Random r = new Random();
   string validCode = "";
   for (int i = 0; i < 4; i++)
   {
    int number = r.Next(0, yzm.Length);
    validCode += yzm[number];
   }
   // Draw a picture 
   using(Bitmap b = new Bitmap(150, 30))
   {
    using (Graphics g = Graphics.FromImage(b))
    using(Font f = new Font(" Song Style ", 20))
    {
     g.Clear(Color.Yellow);// Clears the entire painting surface and fills it with the specified background color 
     g.DrawString(validCode, f, Brushes.Red, new PointF(0, 0));// Start drawing 
     // Foreground point 
     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);
     }
     // Backview line 
     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);// Save to output stream 
   }

Reference:

asp. net Methods for Generating Verification Codes (1)

Implementation method of webform image watermarking and image verification code developed by Asp. net


Related articles: