asp. net watermark specific implementation code

  • 2020-07-21 07:28:17
  • OfStack

The watermark is to prevent us from stealing our images.

Two ways to achieve watermark effect

1) Watermark can be added when the user uploads.

a) Benefits: The server sends the image directly to the customer each time the user reads it, as opposed to the two methods.

Faults: corrupted original image.

2) Through the global 1 general processor, when the user requests the image, add watermark.

a) Benefit: The original image is not destroyed

b) Disadvantages: Every time the user requests, the requested image needs to be watermarked, which wastes the resources of the server.

Code to achieve the second way:


using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Web;   
using System.Drawing;   
using System.IO;   

namespace BookShop.Web   
{   
    public class WaterMark : IHttpHandler   
    {   

        private const string WATERMARK_URL = "~/Images/watermark.jpg";        // The watermark image    
        private const string DEFAULTIMAGE_URL = "~/Images/default.jpg";<span style="white-space:pre">   </span>      // The default image    
        #region IHttpHandler  Members of the    

        public bool IsReusable   
        {   
            get { return false; }   
        }   

        public void ProcessRequest(HttpContext context)   
        {   

            //context.Request.PhysicalPath  // Gets the physical path to the file requested by the user    

            System.Drawing.Image Cover;   
            // Determines whether a file exists in the physical path of the request    
            if (File.Exists(context.Request.PhysicalPath))   
            {   
                // Load the file    
                Cover = Image.FromFile(context.Request.PhysicalPath);   
                // Loaded watermark image    
                Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));   
            // Get a drawing image from the book cover    
                Graphics g = Graphics.FromImage(Cover);   
                // in image Watermark on top    
                g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.Height - watermark.Height,    
[csharp] view plaincopy 
watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);   
                // The release of the canvas    
                g.Dispose();   
                // Release watermark image    
                watermark.Dispose();   
            }   
            else  
            {   
                // Load the default image    
                Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL));   
            }   
            // Set the output format    
            context.Response.ContentType = "image/jpeg";   
            // Store the image in the output stream    
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);   
            Cover.Dispose();   
            context.Response.End();   
        }   

        #endregion   
    }   
}


Related articles: