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>

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

  1. kristinglasheen October 10, 2014 / 6:47 am

    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 comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.