Resources » Code Snippets » Graphics

Dynamic Watermarking Images in C#.NET


Posted Date: 22-Jun-2008  Last Updated:   Category: Graphics    
Author: Member Level: Gold    Points: 10



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);


Did you like this resource? Share it with your friends and show your love!

Responses to "Dynamic Watermarking Images in C#.NET"
Guest Author: Ilya Voronov     15 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.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India