Adsence750x90

Friday, June 20, 2008

Creating runtime watermark on image using C# and ASP.NET

Creating watermark on image using C# and ASP.NET

C# Code
//Creating image with watermark
//using System.IO;
//using System.Drawing.Drawing2D;
//using System.Drawing.Imaging;
System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath("image.jpg"));//From File
int height = objImage.Height;//Actual image width
int width = objImage.Width;//Actual image height
System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);// create bitmap with same size of Actual image
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);
//Creates a System.Drawing.Color structure from the four ARGB component
//(alpha, red, green, and blue) values. Although this method allows a 32-bit value
// to be passed for each component, the value of each component is limited to 8 bits.
//create Brush
SolidBrush brush = new SolidBrush(Color.FromArgb(113, 255, 255, 255));
//Adding watermark text on image
g.DrawString("Raju.M C# Programmer India @ WaterMark sample", new Font("Arial", 18, FontStyle.Bold), brush,100, 100);
//save image with Watermark image/picture
bitmapimage.Save("watermark-image.jpg");
Download Source

4 comments:

Anonymous said...

Just what I was looking for. Your the man!

Raju.M said...

i update the download link.if you want, you can download from that link

Teri Yaad Yaad said...

Hello Raju,

Nice snippet, I have a question, why we should save the images check this statement at end of code : bitmapimage.Save("watermark-image.jpg");

We have a saved image and we made it water mark image now, why we cant show it without saving. Might be it is confusing but whats the benefit to having two image file of a same image one is without water mark and another is with water mark. Here is the simple scenario:

1. Make a water mark on the saved image
2. While anyone want to view a particular image form image library a watermarked image will be displayed.

Raju.M said...

Hi Teri Yaad Yaad,
Thanks for your comment. Here I just show how to save that image nothing else.
If you want to know more please check my other posts
http://www.codeproject.com/Articles/126421/Image-Handling-In-ASP-NET.aspx

or

http://makhaai.blogspot.com/2010/11/image-handling-in-aspnet-part-1.html