Have you ever wanted to watermark your images in a C# application? Write your own little copyright on the photo and prevent those lamahs from actually ripping it off your site and using it for their own dastardly deeds? Ever wonder why I talk with questions and sound like I am an infommercial? Neither do I but after seeing a great question on the boards about watermarking, I thought I would write out a nice little function to make the whole process easier! Lets get to watermarking on this episode of the Programming Underground!
Watermarking is the process of making semi-transparent text or images on an image so that others can’t steal it and use it as their own. You find this technology useful for sites that sell custom graphics, screenshots where the owner doesn’t want you using the images without permission, or to simple let everyone know that a graphic was created or distributed from you.
Typically this watermarking is text but can also be images. For our little demo below I have gone ahead and made a watermarking function in C# for text only. I may add a part 2 to this entry to show you how you can watermark images as well. So stay tuned for that.
The trick is a function called FromArgb of the Color structure. This function takes in a varying number of parameters, but the first parameter is one of opacity followed by a series of RGB color values. By setting this opacity parameter, and using something like a color of 255 for the RGB values (essentially white), you can give the faint appearance of text and making it transparent. In our function we use the value 113, but you can certainly adjust this number to make it more or less transparent. Now of course if your picture has a lot of white you may actually want to change the RGB values to another color like gray or something.
Here is our little function…
// Takes a graphics object, a font object for our text, and the text we want to write. // Then writes it to the handle as a watermark private void writeWaterMark(Graphics graphicsHandle, int x, int y, Font ourFont, String text) { StringFormat strFormatter = new StringFormat(); SolidBrush transBrush = new SolidBrush(Color.FromArgb(113, 255, 255, 255)); // Drawstring the transparent text to the Graphics context at x,y position. graphicsHandle.DrawString(text, ourFont, transBrush, new PointF(x, y), strFormatter); }
I designed this little function to give you various options including positioning, choosing your own font and size etc. You can expand it further to allow various alignment etc using the formatter supplied in the function. This little function requires that we include the System.Drawing namespace in our project and then calling this function with a graphics object (which could be from a form, image or any drawing service that allows you to get access to its graphics canvas), an x and y position of where on the canvas you want to draw to, a Font object which specifies the font family and size as well as the text we want to write.
So for instance if I want to use this method in my Form’s paint event so that it draws the watermark any time the form gets repainted, then I could use…
// Write a Copyright Dream In Code watermark on our form that is Arial 14pt bold at position 100,100 writeWaterMark(e.Graphics, 100, 100, new Font("Arial", 14, FontStyle.Bold), "Copyright Dream.In.Code.net");
It is as simple as that! You can use this to also take images, write the text on them, then save the image to your disk drive. Throw it into a loop and you could even do batch watermarking of images in certain folders etc.
Next time you want to watermark that porn (You know who you are *cough* Pyscho *cough*) you will be able to do it with style. And like I said, I may do a part 2 where we watermark with images which should be pretty fun. I hope you enjoy the little function and find it useful. Enjoy! 🙂