Watermark is to prevent us from stealing our pictures.
two ways to realize the effect of watermark
1) can add watermark when the user uploads.
A the benefits: compared to 2 methods, each time the user read this picture, the server directly sent to the customer.
b) Disadvantage: The original picture was destroyed.
2 through the Global general processing program, when the user requests this picture, add watermark.
A) Benefits: The original picture was not destroyed
b) Disadvantage: Every time the user requests, the requested image should be added watermark processing, waste of the server resources.
The second way code implements:
Copy Code code as follows:
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"; Watermark Picture
Private Const string Defaultimage_url = "~/images/default.jpg"; <span style= "White-space:pre" > </span>// Default picture
#region IHttpHandler Members
public bool IsReusable
{
get {return false;}
}
public void ProcessRequest (HttpContext context)
{
Context. Request.PhysicalPath//Get the file physical path requested by the user
System.Drawing.Image Cover;
To determine whether a file exists in the physical path of the request
if (file.exists context. Request.PhysicalPath))
{
Loading files
Cover = Image.FromFile (context. Request.PhysicalPath);
Load watermark Picture
Image watermark = image.fromfile (context. Request.mappath (Watermark_url));
Through the cover of the book to get the drawing right like
Graphics g = graphics.fromimage (Cover);
Draw a watermark on image
G.drawimage (Watermark, New Rectangle (Cover.width-watermark). Width, Cover.height-watermark. Height,
Copy Code code as follows:
Watermark. Width, Watermark. Height), 0, 0, Watermark. Width, Watermark. Height, GraphicsUnit.Pixel);
Free Canvas
G.dispose ();
Release watermark Picture
Watermark. Dispose ();
}
Else
{
Load default picture
Cover = Image.FromFile (context. Request.mappath (Defaultimage_url));
}
Set output format
Context. Response.ContentType = "Image/jpeg";
Save picture to output stream
Cover.save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);
Cover.dispose ();
Context. Response.End ();
}
#endregion
}
}