Dynamic Watermarking Images in C#.NET

The code sample is a powerful way to watermark images dynamically. The code below watermarks ootyflower.jpg image by creating a bitmap image of the same size and writing a text across the image.

The code uses System.Drawing and System.Graphics namespaces.


//Creating Dynamic Watermark on image
System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath("ootyflower.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 Kerala,India.Dynamic WaterMark sample", new Font("Arial", 18, System.Drawing.FontStyle.Bold), brush, 0, 100);
//save image with Watermark image/picture
//bitmapimage.Save("watermark-image.jpg"); //if u want to save image
Response.ContentType = "image/jpeg";
bitmapimage.Save(Response.OutputStream, ImageFormat.Jpeg);


Comments

Guest Author: Ilya Voronov15 Nov 2012

Clear and simple, thanks!
Actually, you don't need to create a new bitmap, you can get Graphis right from the original bitmap.