ASP.NET网站开发——用户控件与HttpHandle

用户控件与HttpHandle

一、用户控件

含义:用户控件是能够在其中放置标记和web服务器控件的容器,可以被看作一个独立的单元,拥有自己的属性和方法,并可被放入到ASPX页面上,其工作原理与ASP.NET页面非常相似。也可以这样理解:当一个web窗体被当作server控件使用时,这个web窗体便是用户控件。

1.创建用户控件


例如:设计界面代码如下


在按钮里些事件

 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);
        }

结果如下:


二、HttpHandler

1.在添加中添加“一般处理程序”


2.封面数字水印的实现(指定Handler)

设计界面如下:


在Handler.ashx添加新引用using System.Drawing.Imaging

代码如下:

 
 
 public class Handler1 : IHttpHandler
    {
        private string IMGS = "~/ProductImgs/";
        public void ProcessRequest(HttpContext context)
        {
            //定义新的图片
            Image img;
            //获取图片物理路径
            string path = context.Request.MapPath(IMGS+context.Request.QueryString["id"]+".jpg");
            //判断图片的物理路径是否存在
            if (File.Exists(path))
            { 
                //加载图片
                img = Image.FromFile(path);
                //定义画布
                Graphics graphics = Graphics.FromImage(img);
                //定义字体,合成水印图片
                graphics.DrawString("测试文本",new Font("黑体",20),Brushes.Red,img.Width-80,img.Height-20);
                //释放画布 
                graphics.Dispose();
            }
            else
	{
                img=null;
	}
            //定义输出类型 context.Request.PhysicalPath
            context.Response.ContentType="image/jpeg";
            //保存图片到输出流
            img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
            //释放画布
            img.Dispose();
            //输出结束
            context.Response.End();
            
        }

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


在浏览器中查看如下:


3.数字水印的实现(全局Handler方式)

修改代码如下:

 public class Handler1 : IHttpHandler
    {
  //      private string IMGS = "~/ProductImgs/";
        public void ProcessRequest(HttpContext context)
        {
            //定义新的图片
            Image img;
            //获取图片物理路径
   //         string path = context.Request.MapPath(IMGS+context.Request.QueryString["id"]+".jpg");
            //判断图片的物理路径是否存在
            if (File.Exists(context.Request.PhysicalPath))
            { 
                //加载图片
                img = Image.FromFile(context.Request.PhysicalPath);
                //定义画布
                Graphics graphics = Graphics.FromImage(img);
                //定义字体,合成水印图片
                graphics.DrawString("测试文本",new Font("黑体",20),Brushes.Red,img.Width-80,img.Height-20);
                //释放画布 
                graphics.Dispose();
            }
            else
	{
                img=null;
	}
            //定义输出类型 
            context.Response.ContentType="image/jpeg";
            //保存图片到输出流
            img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
            //释放画布
            img.Dispose();
            //输出结束
            context.Response.End();
            
        }


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

结果如下:


猜你喜欢

转载自blog.csdn.net/qq706352062/article/details/79849507
今日推荐