How to realize watermark _ practical technique in asp.net

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.