How to use HttpModule and HttpHandler object to add watermark and verification code to image

1, How to add watermark to image

Last time we talked about using HttpModule and HttpHandler object anti-theft chain, the principle of adding watermark to image is the same.
1. Create the first website and prepare its image resources
2. Add HttpHandler class to the website, inherit IHttpHandler interface, and write watermark function.
1> The code is as follows:

 //Gets the path to add the picture
            string path = context.Request.PhysicalPath;
            System.Drawing.Image Cover;
            //Determine whether the physical path of the request exists
            if (File.Exists(path))
                //Loading picture files
                Cover = Image.FromFile(path);
                //Define canvas
                Graphics graphics = Graphics.FromImage(Cover);
                graphics.DrawString("Exclusive to Xiaohe", new Font("Microsoft YaHei ", 20), Brushes.Red, Cover.Width - 125, Cover.Height - 15);
                //Release canvas

                //If the image does not exist, the default image is displayed
                Cover = Image.FromFile(pic2);
            //Format the output image
            context.Response.ContentType = "image/jepg";
            //Save the modified image into the output stream
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //Release picture
            //Terminate output

2> Configuration related profiles:

      <add verb="*" path="images/*" type="test.Class1" name="sd"/>

Sometimes, in order to prevent malicious network behaviors such as password cracking, ticket swiping, forum watering, page swiping and so on, it is effective to prevent a hacker from constantly trying to log in to a specific registered user with a specific program brute force cracking method. So, we use CAPTCHA to prevent hackers from stealing passwords.

2, How to generate captcha

1.web scope: PageContext (page domain), request, session, servletContext (application domain)
2. Use HttpHandler to generate the verification code
3. Save the verification code to the session scope
4. Submit the program to obtain the verification code submitted by the user
5. Comparison of user authentication code and session scope

The code is as follows:

 //Generate random number object:
            Random random = new Random();
            string str = "123456789qweqwertyuiopasdfghjklzxcvbnm";
            string num = null;
            for (int i = 0; i < 5; i++)
                num += str[random.Next(0, str.Length)];
            //Save the verification code to session
            context.Session["code"] = num.ToLower();
            CreateImages(context, num);
        public void CreateImages(HttpContext context, string checkCode)
            int iwidth = (int)(checkCode.Length * 13);
            Bitmap bitmp = new Bitmap(iwidth, 22);
            Graphics graphics = Graphics.FromImage(bitmp);
            //define color
            Color[] c = new Color[] { Color.Blue, Color.Brown, Color.Red, Color.Purple };
            //Define font
            string[] s = { "Song style", "Microsoft YaHei " };
            Random rand = new Random();
            //Random output noise
            for(int i = 0; i < 50; i++)
                int x = rand.Next(bitmp.Width);
                int y= rand.Next(bitmp.Height);
                graphics.DrawRectangle(new Pen(Color.LightCyan, 0), x, y, 1, 1);
            //Output captcha characters of different fonts and colors
            for(int i = 0; i < checkCode.Length; i++)
                int cindex = rand.Next(7);
                int findex= rand.Next(5);
                Font f =  new Font(s[findex], 10, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                    ii = 2;
                graphics.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), ii);
            //Draw a border
            graphics.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0, bitmp.Width - 1, bitmp.Height - 1);
            //Output to browser
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmp.Save(ms, ImageFormat.Jpeg);
            context.Response.ContentType = "image/gif";

noise mainly refers to the rough part of the image produced in the process of CCD (CMOS) taking light as the received signal and outputting it. It also refers to the foreign pixels that should not appear in the image, which are usually generated by electronic interference.
It looks as if the image has been smudged and covered with small rough spots. If you use a personal computer to zoom out the high-quality image and look at it later, you may not notice it. However, if the original image is enlarged, then there will be no color (false color), this false color is image noise. As shown in the figure:

And here the noise is in the captcha image, some designated color points, interference program automatically analyzes the captcha pixels.

Tags: C# ASP.NET Programmer

Posted by Ruzzas on Thu, 03 Jun 2021 03:45:35 +0930