ASP.NET website development - user controls and HttpHandle

User Control and HttpHandle

1. User Controls

Meaning: A user control is a container in which markup and web server controls can be placed. It can be seen as an independent unit, with its own properties and methods, and can be placed on an ASPX page. It works the same as ASP.NET The pages are very similar. It can also be understood this way: when a web form is used as a server control, the web form is a user control.

1. Create a User Control


For example: the design interface code is as follows


some events in the button

protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(Button1.Text+":"+TextBox1.Text);
        }
        public string Settext
        {
            get
            {
                return Button1.Text;
            }
            set
            {
                Button1.Text = value;
            }
        
        }
        public void show()
        {
            Response.Write(Button1.Text);
        }

The result is as follows:


二、HttpHandler

1. Add "general handler" in add


2. Implementation of cover digital watermark (specify Handler)

The design interface is as follows:


Add new reference using System.Drawing.Imaging in Handler.ashx

code show as below:

 
 
public class Handler1 : IHttpHandler
    {
        private string IMGS = "~/ProductImgs/";
        public void ProcessRequest(HttpContext context)
        {
            // define a new image
            Image img;
            //Get the physical path of the picture
            string path = context.Request.MapPath(IMGS+context.Request.QueryString["id"]+".jpg");
            / / Determine whether the physical path of the image exists
            if (File.Exists(path))
            {
                //load image
                img = Image.FromFile(path);
                //define the canvas
                Graphics graphics = Graphics.FromImage(img);
                //Define the font and synthesize the watermark image
                graphics.DrawString("Test Text",new Font("Black Body",20),Brushes.Red,img.Width-80,img.Height-20);
                // release the canvas
                graphics.Dispose();
            }
            else
	{
                img=null;
	}
            //Define the output type context.Request.PhysicalPath
            context.Response.ContentType="image/jpeg";
            // save the image to the output stream
            img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
            // release the canvas
            img.Dispose();
            // end of output
            context.Response.End();
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


View in your browser as follows:


3. Implementation of digital watermark (global Handler method)

Modify the code as follows:

public class Handler1 : IHttpHandler
    {
  //      private string IMGS = "~/ProductImgs/";
        public void ProcessRequest(HttpContext context)
        {
            // define a new image
            Image img;
            //Get the physical path of the picture
   //         string path = context.Request.MapPath(IMGS+context.Request.QueryString["id"]+".jpg");
            / / Determine whether the physical path of the image exists
            if (File.Exists(context.Request.PhysicalPath))
            {
                //load image
                img = Image.FromFile(context.Request.PhysicalPath);
                //define the canvas
                Graphics graphics = Graphics.FromImage(img);
                //Define the font and synthesize the watermark image
                graphics.DrawString("Test Text",new Font("Black Body",20),Brushes.Red,img.Width-80,img.Height-20);
                // release the canvas
                graphics.Dispose();
            }
            else
	{
                img=null;
	}
            //define the output type
            context.Response.ContentType="image/jpeg";
            // save the image to the output stream
            img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
            // release the canvas
            img.Dispose();
            // end of output
            context.Response.End();
            
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

The result is as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325909768&siteId=291194637