• \r\n \r\n 
    \r\n  <\/asp:TextBox>\r\n  \r\n  \r\n <\/div>\r\n <\/form>\r\n<\/body>\r\n<\/html><\/pre>

    I hope this article will be helpful to everyone asp.net Programming helps. <\/p>\n

    For more articles on how to simply generate verification codes in asp.net, please pay attention to the PHP Chinese website! <\/p>"}

    国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

    Home Backend Development C#.Net Tutorial A simple method to generate verification code in asp.net

    A simple method to generate verification code in asp.net

    Jan 13, 2017 pm 03:00 PM

    The example in this article describes how asp.net simply generates verification codes. Share it with everyone for your reference, the details are as follows:

    1. Create a new general processing program

    namespace WebApplication1
    {
     /// <summary>
     /// $codebehindclassname$ 的摘要說(shuō)明
     /// </summary>
     [WebService(Namespace = "http://tempuri.org/")]
     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
     public class Handler1 : IHttpHandler, IRequiresSessionState
     {
      public void ProcessRequest(HttpContext context)
      {
       context.Response.ContentType = "image/gif";
       //建立Bitmap對(duì)象,繪圖
       Bitmap basemap = new Bitmap(100, 30);
       Graphics graph = Graphics.FromImage(basemap);
       graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 100, 30);
       Font font = new Font(FontFamily.GenericSerif, 24, FontStyle.Bold, GraphicsUnit.Pixel);
       Random r = new Random();
       string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
       string letter;
       StringBuilder s = new StringBuilder();
       //添加隨機(jī)的五個(gè)字母
       for (int x = 0; x < 5; x++)
       {
        letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
        s.Append(letter);
        graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 19, r.Next(0, 8));
       }
       //混淆背景
       Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
       for (int x = 0; x < 6; x++)
        graph.DrawLine(linePen, new Point(r.Next(0, 99), r.Next(0, 29)), new Point(r.Next(0, 99), r.Next(0, 29)));
       //將圖片保存到輸出流中  
       basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
       context.Session["CheckCode"] = s.ToString(); //如果沒(méi)有實(shí)現(xiàn)IRequiresSessionState,則這里會(huì)出錯(cuò),也無(wú)法生成圖片
       context.Response.End();
      }
      public bool IsReusable
      {
       get
       {
        return false;
       }
      }
     }
    }

    2. Front-end code

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
     <title></title>
    </head>
    <body>
     <form id="form1" runat="server">
     <div>
      <asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
      <img id="imgCode" alt="看不清?點(diǎn)擊換一張" src="Handler1.ashx" style="cursor:pointer" onclick="this.src=this.src+&#39;?&#39;" /><br />
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
     </div>
     </form>
    </body>
    </html>

    I hope this article will be helpful to everyone asp.net Programming helps.

    For more articles on how to simply generate verification codes in asp.net, please pay attention to the PHP Chinese website!

    Statement of this Website
    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

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the significance of the yield keyword in C# for creating iterators? What is the significance of the yield keyword in C# for creating iterators? Jun 19, 2025 am 12:17 AM

    TheyieldkeywordinC#simplifiesiteratorcreationbyautomaticallygeneratingastatemachinethatenableslazyevaluation.1.Itallowsreturningitemsoneatatimeusingyieldreturn,pausingexecutionbetweeneachitem,whichisidealforlargeordynamicsequences.2.yieldbreakcanbeus

    What is Dependency Injection (DI), and how can it be implemented in C# (e.g., using built-in DI in ASP.NET Core)? What is Dependency Injection (DI), and how can it be implemented in C# (e.g., using built-in DI in ASP.NET Core)? Jun 30, 2025 am 02:06 AM

    DependencyInjection(DI)inC#isadesignpatternthatenhancesmodularity,testability,andmaintainabilitybyallowingclassestoreceivedependenciesexternally.1.DIpromotesloosecouplingbydecouplingobjectcreationfromusage.2.Itsimplifiestestingthroughmockobjectinject

    What is the purpose of the IDisposable interface and the using statement in C# for resource management? What is the purpose of the IDisposable interface and the using statement in C# for resource management? Jun 27, 2025 am 02:18 AM

    The role of IDisposable and using in C# is to efficiently and deterministically manage unmanaged resources. 1. IDisposable provides Dispose() method, so that the class can clearly define how to release unmanaged resources; 2. The using statement ensures that Dispose() is automatically called when the object is out of scope, simplifying resource management and avoiding leakage; 3. When using it, please note that the object must implement IDisposable, can declare multiple objects, and should always use using for types such as StreamReader; 4. Common best practices include not relying on destructors to clean up, correctly handling nested objects, and implementing the Dispose(bool) pattern.

    How do lambda expressions and LINQ (Language Integrated Query) enhance data manipulation in C#? How do lambda expressions and LINQ (Language Integrated Query) enhance data manipulation in C#? Jun 20, 2025 am 12:16 AM

    LambdaexpressionsandLINQsimplifydatamanipulationinC#byenablingconcise,readable,andefficientcode.1.Lambdaexpressionsallowinlinefunctiondefinitions,makingiteasiertopasslogicasargumentsforfiltering,transforming,sorting,andaggregatingdatadirectlywithinme

    What are nullable reference types (NRTs) in C# 8 , and how do they help prevent NullReferenceException? What are nullable reference types (NRTs) in C# 8 , and how do they help prevent NullReferenceException? Jun 21, 2025 am 12:36 AM

    Nullablereferencetypes(NRTs)inC#8 helpcatchNullReferenceExceptionerrorsatcompiletimebymakingreferencetypesnon-nullablebydefault,requiringexplicitdeclarationfornullability.NRTsmustbeenabledeitherinthe.csprojfilewithenableoratthetopofa.csfileusing#null

    What are some common pitfalls or anti-patterns to avoid when developing with C#? What are some common pitfalls or anti-patterns to avoid when developing with C#? Jun 23, 2025 am 12:05 AM

    Four common "anti-pattern" problems in C# development need to be avoided. First, the unreasonable use of async/await leads to deadlocks or performance degradation. We should adhere to the principle of full asynchronousness, configure ConfigureAwait(false) and standardize naming; second, excessive dependence on var affects readability, and explicitly declare and unify team specifications when the type is unclear; third, the incorrect use of Dispose and resource management causes leakage, and the use statement should be used correctly and the IDisposable standard mode should be implemented; fourth, the abuse of static classes or singletons causes testing difficulties, and priority should be given to dependency injection, statelessness, or the life cycle managed by containers. Avoiding these misunderstandings can significantly improve code quality and maintenance.

    How can Span and Memory be used in C# to optimize memory usage and reduce allocations? How can Span and Memory be used in C# to optimize memory usage and reduce allocations? Jun 18, 2025 am 12:11 AM

    Span and Memory improve C# performance by reducing memory allocation. 1. Span avoids array copying and provides light references to existing memory, which is suitable for parsing binary protocols, string operations and high-performance buffer management; 2. Memory supports passing memory slices across asynchronous methods, which is suitable for scenarios where more flexible life cycles are required; 3. Both reduce GC pressure, optimize performance by reusing buffers and avoiding temporary copying; 4. Span is limited to use on the stack and cannot be stored in classes or used in asynchronous methods. Be careful to avoid reassignment operations such as calling.ToArray().

    Can you explain the SOLID principles and their application in C# object-oriented design? Can you explain the SOLID principles and their application in C# object-oriented design? Jun 25, 2025 am 12:47 AM

    SOLID principle is five design principles to improve code maintainability and scalability in object-oriented programming. They are: 1. The single responsibility principle (SRP) requires that the class only assumes one responsibility, such as separating report generation and email sending; 2. The opening and closing principle (OCP) emphasizes that the extension is supported through interfaces or abstract classes without modifying the original code, such as using the IShape interface to realize area calculation of different graphics; 3. The Richter replacement principle (LSP) requires that the subclass can replace the parent class without destroying logic, such as Square should not mistakenly inherit Rectangle, resulting in abnormal behavior; 4. The interface isolation principle (ISP) advocates the definition of fine-grained interfaces, such as split printing and scanning functions to avoid redundant dependencies; 5. The dependency inversion principle (DIP) advocates the

    See all articles