[ASP.NET] to establish a watermark of page

No watermark on the original system page report, now to add a watermark to introduce the next step in the process of practice.


Foreword

No watermark on the original system page report, now to add a watermark to introduce the next step in the process of practice.

practice

Intuitive idea is to set the base map page, as follows,


Q. So, if you want to map dynamically generated out of it (our demand is to show users and the current time)?

Then dynamically generated through FIG procedure, as follows,


Dynamically generating a bit image file out through a transparent GetWatermark.ashx, the following procedure,


/// 
/// 动态产生有点透明的图片
/// 
/// 
  
  
   
   
/// 因为要存取Session的数据,所以实践 IRequiresSessionState Interface
/// 
  
  
public class GetWatermark : IHttpHandler, IRequiresSessionState
{
    /// 
    /// 产生Watermark的图片
    /// 
    /// 
    /// 
  
  
   
   
    /// 
  
  
    public void ProcessRequest(HttpContext context)
    {
        const string DefaultFontName = @"细明体";
        int useWay = 0;
        //如果传进来的是2,就使用黑色,不然就用淡色
        int.TryParse(context.Request["way"] as string, out useWay);
         
        //设定输出为gif档
        context.Response.ContentType = "image/gif";
        int fontSize = 30;
        int bmpWidth = 500;
        int bmpHeight = 400;
        string watermark = @"使用者:{0},日期:{1},时间:{2}";  
        //建立Bitmap
        Bitmap bmp = new Bitmap(bmpWidth, bmpHeight);
        //设定使用者及时间,并将逗号改成换行符号
        string watermarks = string.Format(watermark, context.Session["User_Name"] as string, 
DateTime.Now.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture), 
DateTime.Now.ToString("HH:mm:ss")).Replace(",", Environment.NewLine);
        //建立Graphics
        Graphics canvas = Graphics.FromImage(bmp);
        //设定透明的Brush
        SolidBrush watermarkBrush;
        if (useWay == 2)
        {
            //如果是用盖到画面上的方式,Color就直接用黑色
            watermarkBrush = new SolidBrush(Color.Black);
        }
        else
        {
            //如果是用底图的话,Color就用淡一点
            watermarkBrush = new SolidBrush(Color.FromArgb(128, 221, 221, 255));
        }

        //设定底图为白色
        SolidBrush whiteBrush = new SolidBrush(Color.White);
        //将底图画成白色
        canvas.FillRectangle(whiteBrush, 0, 0, bmp.Width, bmp.Height);
        //因为要由下往上画,所以将原点设定成下方
        canvas.TranslateTransform(50, bmpHeight - 200);
        //设定浮水印的字型及大小
        Font f = new Font(DefaultFontName, fontSize, FontStyle.Bold);
        //设定旋转的角度为330
        canvas.RotateTransform(330);
        //将浮水印画上去
        canvas.DrawString(watermarks, f, watermarkBrush, fontSize, 0, StringFormat.GenericTypographic);
        //将图档输出去
        bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
        
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

image

Try using a test page, showing the base map in the middle of the page, I feel pretty good.

WatermarkNoInh.aspx



    
  
  

WatermarkNoInh.aspx.CS


public partial class WatermarkNoInh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["User_Name"] = "郭小玉";
        DataTable myData = new DataTable();
        myData.Columns.Add("c1", typeof(int));
        myData.Columns.Add("c2", typeof(string));
        for (int i = 0; i < 100; i++)
        {
            myData.Rows.Add(i, i.ToString());
        }
        GridView1.DataSource = myData;
        GridView1.DataBind();
    }
}

image

At this time set the GridView apply Style, base maps were found to cover off the BackColor, as follows,



  
  
    
   
   
    
   
   
    
   
   

  
  

image

Therefore, when the Render again, and then through all objects JQuery background-color set to be transparent, as follows,


$(document).ready(function () 
{ 
    $('*').css('background-color', 'transparent');
});

Q. That does not have a plus with no increase Style Style Like it?

Yes, but, if it does not want to change the original program of Style, so you can use!

Otherwise they use way to the top of the cover picture (UseWatermark = 2).

Q. What if some pages have a watermark reports, some do not it?

It produces WatermarkBasePage establish a watermark, and then went to the watermark Page inherit it (if you already have the underlying Page, the Code can be directly applied to the bottom of Page), the program is as follows (refer to ASP. NET - C # Application Environment Backsplash),


public class WatermarkBasePage : System.Web.UI.Page 
{
    /// 
    /// 是否要启用浮水印
    /// 
    /// 
  
  
   
   
    /// 0:不使用浮水印
    /// 1:使用底图方式的浮水印
    /// 2:使用z-index方式的浮水印
    /// 
  
  
    public int UseWatermark
    {
        get;
        set;
    }
    protected override void OnPreRender(EventArgs e)
{
 base.OnPreRender(e);
 switch (UseWatermark)
 {
  case 1:
   //要将页面上所有对象的背景色取消掉
   //这样底图才不会被盖掉
   ClientScript.RegisterClientScriptBlock(this.GetType(),  "UseWatermark1",
     @" $(document).ready(function ()
       { $('*').css('background-color', 'transparent');
      if($('#imgWatermark').length==0){
      $('body').prepend(''); 
        }
       $(window).resize(); 
     });
      $(window).resize(function() { 
       $('#imgWatermark').css('z-index', '-1').css('opacity','0.1').css('width', $('body').width()-20).css('height', $('body').height()-20);
      });", true);
   break;
  case 2:
   //使用图片盖到画面上,并随画面调整图片大小
   ClientScript.RegisterClientScriptBlock(this.GetType(), "UseWatermark2",
     @" $(document).ready(function ()
       {  
      if($('#imgWatermark').length==0){
      $('body').prepend(''); 
        }
       $(window).resize(); 
     });
      $(window).resize(function() { 
       $('#imgWatermark').css('z-index', '999').css('opacity','0.1').css('width', $('body').width()-20).css('height', $('body').height()-20);
      });", true);
   break;
 }
}
    

In succession to use self Page WatermarkBasePage, and is set to 1 or 2 UseWatermark property (1: watermark basemap way, 2: watermark using z-index mode), will produce the dynamic map, and set the base map for the web page.


public partial class WatermarkTest1 : WatermarkBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //要使用底图方式的浮水印
        UseWatermark = 1;
        Session["User_Name"] = "郭小玉";
        DataTable myData = new DataTable();
        myData.Columns.Add("c1", typeof(int));
        myData.Columns.Add("c2", typeof(string));
        for (int i = 0; i < 100; i++)
        {
            myData.Rows.Add(i,  i.ToString());
        }
        GridView1.DataSource = myData;
        GridView1.DataBind();
    }
}

image


public partial class WatermarkTest1 : WatermarkBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //2:使用z-index方式的浮水印
        UseWatermark = 2;
        Session["User_Name"] = "郭小玉";
        DataTable myData = new DataTable();
        myData.Columns.Add("c1", typeof(int));
        myData.Columns.Add("c2", typeof(string));
        for (int i = 0; i < 100; i++)
        {
            myData.Rows.Add(i,  i.ToString());
        }
        GridView1.DataSource = myData;
        GridView1.DataBind();
    }
}

image

in conclusion

More offers 2 watermark ways, each according to necessity Page to set it. You can also go adjusted according to your needs, such as writing watermark way, from top left to bottom right, or draw a circle.

PS. Thanks 655 Fred's Support.

test program

Watermarks.zip 2012/09/10 changed by using the drawing and z-Index is determined is placed above or below the control.


Original: Large column  [ASP.NET] to establish a watermark of page


Guess you like

Origin www.cnblogs.com/chinatrump/p/11490877.html