How to Create “watermark” Text on Image in asp.net?



Hi
If you want to protect your image being reuse by other person, then the best way to protect image by keeping watermark text on image.

Here is the few steps to do this task

Step1: Keep some image on Default page like this

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="images/Koala.jpg" height="500px" width="600px" />
</div>
</form>
</body>
</html>

Step2: Create one Class i.e WaterMark in appcode folder and write the code like this

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mime;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for ImageHandler
/// </summary>
public class WaterMark : IHttpHandler
{

public string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}

public ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}

protected byte[] WatermarkImage(HttpContext context)
{

byte[] imageBytes = null;
if (File.Exists(context.Request.PhysicalPath))
{
// Normally you’d put this in a config file somewhere.
string watermark = "https://chandradev819.wordpress.com/&quot;;

Image image = Image.FromFile(context.Request.PhysicalPath);

Graphics graphic;
if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
{

graphic = Graphics.FromImage(image);
}
else
{

Bitmap indexedImage = new Bitmap(image);
graphic = Graphics.FromImage(indexedImage);

// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;

Font myFont = new Font("Arial", 20);
SolidBrush brush = new SolidBrush(Color.FromArgb(80, Color.White));

//This gets the size of the graphic

SizeF textSize = graphic.MeasureString(watermark, myFont);

// Code for writing text on the image.

PointF pointF = new PointF(430, 710);
graphic.DrawString(watermark, myFont, brush, pointF);

using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetImageFormat(context.Request.PhysicalPath));
imageBytes = memoryStream.ToArray();
}

}
return imageBytes;
}

#region IHttpHandler Members

public bool IsReusable
{
get { return false; }
}

public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.PhysicalPath);
byte[] imageBytes = WatermarkImage(context);
if (imageBytes != null)
{
context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
else
{

context.Response.StatusCode = 404;
}
context.Response.End();
}

#endregion
}

Step3: In web confi file add this line between tag like this

<httpHandlers>

<add verb="*" path="*.jpg,*.gif" type="WaterMark" />
</httpHandlers>

Advertisements
Posted in Asp.net. 1 Comment »

One Response to “How to Create “watermark” Text on Image in asp.net?”

  1. kristinglasheen Says:

    Just desire to say your article is as astonishing.
    The clearness in your post is just nice and i can assume
    you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date
    with forthcoming post. Thanks a million and please keep
    up the enjoyable work.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: