Click here to Skip to main content
Click here to Skip to main content

Creating a Watermarked Photograph with GDI+ for .NET

, 25 Sep 2002
Rate this:
Please Sign up or sign in to vote.
This article describes the process of overlaying a Watermark image on top of a photograph using GDI+ and C# for .NET
<!-- Download Links --> <!-- Add the rest of your HTML here -->


Often when placing photographic images on a website it is necessary to permanently overlay a watermark and/or Copyright statement on top of that image. Such an insertion helps to identify the person that took the photograph and indicate the organization that holds the Copyright. Doing this task by hand can become time consuming and is often inconsistent. Using some simple techniques there is an easy way to accomplish this programmatically using C# and GDI+.


I am going to show you a variety of techniques for manipulating an image. The following is a high level list of some of those techniques:

  • Inserting text on top of an image positioned relative to the size on an image
  • Dynamically choosing a System.Drawing.Font size to maximize readability
  • Manipulating the opacity of a String of text
  • Replacing a specific color in a bitmap to achieve transparency
  • Changing the opacity of an image through a 5x5 ColorMatrix

Define Images

Small photo
(Photo courtesy of AP wire)

The first step in this process is to load a photographic image for which you would like to apply the watermark.  This image can be virtually any size and resolution.  For this example we will use an image that has a width of 449 pixels and a height of 346.  The resolution is 72 dpi.

When the Main method is instantiated, the two variable of type string are defined. The first will define where to find the photograph, watermark and output the new image. The second will define the Copyright string we will use as part of our watermark.

string WorkingDirectory = @"C:\Projects\WaterMark";
string Copyright = "Copyright © 2002 
                 - AP Photo/David Zalubowski";

The following creates an Image object from the specified file and then defines a variable for both its Width and Height. These dimensions are then used to build a Bitmap object with a 24 bits per pixel format for the color data.  Lastly this Bitmap is then used to create a new Graphics object from the specified Bitmap image.

Image imgPhoto = Image.FromFile(WorkingDirectory 
              + "\\watermark_photo.jpg");
int phWidth = imgPhoto.Width; int phHeight =

Bitmap bmPhoto = new Bitmap(phWidth, phHeight, 
bmPhoto.SetResolution(72, 72); 

Graphics grPhoto = Graphics.FromImage(bmPhoto);

Watermark Image This code loads the watermark image that has been saved as a BMP and set with a background color of Green (A=0,R=0,G=255,B=0). Once again it defines a variable for both its Width and Height
(Image courtesy of
Image imgWatermark = new Bitmap(WorkingDirectory
                     + "\\watermark.bmp");
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;

Step #1 - Watermark Text

This code draws the imgPhoto to the Graphics object positioning it (x= 0,y=0) at 100% of its original size. All future drawing will occur on top of the original photograph.

grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
	new Rectangle(0, 0, phWidth, phHeight), 

To maximize the size of the Copyright message we will test 7 different Font sizes to determine the largest possible size we can use for the width of our Photograph.  To effectively do this, we will define an array of integers then iterate through those values measuring the Copyright string in the various point sizes.  Once we have determined the largest possible size we will exit the loop and draw the text.

int[] sizes = new int[]{16,14,12,10,8,6,4};
Font crFont = null; 
SizeF crSize = new	SizeF(); 
for (int i=0 ;i<7; i++)
    crFont = new Font("arial", sizes[i],
    crSize = grPhoto.MeasureString(Copyright,

	if((ushort)crSize.Width < (ushort)phWidth)

Since all photographs will have varying heights, determine a position 5% from the bottom of the image.  Use the Copyright strings height to determine an appropriate y-coordinate for which to draw the string.  Determine its x-coordinate by calculating the centre of the image then define a StringFormat object and set the StringAlignment to Center. 

int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight - 
float xCenterOfImg = (phWidth/2);

StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;

Now that we have all of the necessary positioning coordinates create a SolidBrush with a Color of 60% Black (alpha value of 153).  Draw the Copyright string at the appropriate position offset 1 pixel to the right and 1 pixel down. This offset will create a shadow effect.  Repeat this process using a White Brush drawing the same text directly on top of the previously drawn string.

SolidBrush semiTransBrush2 = 
    new SolidBrush(Color.FromArgb(153, 0, 0,0)); 

    new PointF(xCenterOfImg+1,yPosFromBottom+1), 

SolidBrush semiTransBrush = new SolidBrush(
             Color.FromArgb(153, 255, 255, 255));

    new PointF(xCenterOfImg,yPosFromBottom),  

Step #2 - Watermark Image

Create a Bitmap based on the previously modified photograph. Load this Bitmap into a new Graphic Object
Bitmap bmWatermark = new Bitmap(bmPhoto); 

Graphics grWatermark =

To achieve a translucent watermark we will apply two color manipulations by defining an ImageAttributes object and setting two of its properties. The first step in manipulating the watermark image is to replace the background color with one that is transparent (Alpha=0, R=0, G=0, B=0). To do this we will use a Colormap and define a RemapTable. As previously shown my watermark was defined with a background of 100% Green this will be the color we search for and replace with transparency.

ImageAttributes imageAttributes =
                           new ImageAttributes();
ColorMap colorMap = new ColorMap();

colorMap.OldColor=Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor=Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = {colorMap};


The second color manipulation is used to change the opacity of the watermark. This is done by applying a 5x5 matrix that contains the coordinates for the RGBA space. By setting the 3rd row and 3rd column to 0.3f we achieve a level of opacity.  The result is a watermark which slightly shows the underlying image.

float[][] colorMatrixElements = { 
   new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
   new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
   new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
   new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},
   new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}

ColorMatrix wmColorMatrix = new


With both color manipulations added to the imageAttributes object we can now draw the watermark in the upper right hand corner of the photograph. We will offset the image 10 pixels down and 10 pixels to the left.

int xPosOfWm = ((phWidth - wmWidth)-10);
int yPosOfWm = 10;

    new Rectangle(xPosOfWm,yPosOfWm,wmWidth,

Or last and final step will be to replace the original Image with the new Bitmap, dispose of both Graphic objects then save this Image to the file system.
imgPhoto = bmWatermark;

imgPhoto.Save(WorkingDirectory + "

That's it! Compile the project, run it, and see what happens! The code is fairly straightforward if it all makes sense then these techniques can be used for 100's of different image manipulations. The possibilities are endless.

Revision History

26 Sep 2002 - Initial Revision


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Joel Neubeck
Web Developer
United States United States
Joel is a Microsoft.NET system architect and Director of Development for a leading interactive development firm located in Tempe, Arizona. Our firm designs and develops a wide range of custom web applications which leverage Flash, Ajax, ASP.NET and the Microsoft.Net Framework.
In his free time, he enjoys spending time with his wife and children.

Comments and Discussions

GeneralThanks ! PinmemberGldmelo15-Sep-14 15:18 
QuestionThanks for this great work around. What should I do, if I want to remove this watermark? PinmemberAQEEB PATHAN20-Mar-14 21:18 
Generalafter refactoring Pinmemberniekto23629-Oct-13 3:05 
QuestionRe: after refactoring Pinmemberratul_ctg19-Jan-14 20:27 
SuggestionRe: after refactoring Pinmemberratul_ctg19-Jan-14 21:54 
QuestionGreat Tutorial PinmemberCBroos10-Mar-13 7:39 
GeneralGreat article Pinmemberfilip_andry1-Mar-13 16:42 
AnswerEnhancement and question [modified] PinmemberFransDep12-Feb-13 4:41 
GeneralGreat work! PinmemberMember 77240962-Dec-12 21:49 
GeneralMy vote of 5! PinmemberMember 956657721-Nov-12 2:50 
GeneralMy vote of 5 Pinmemberhcheng_qi29-Dec-11 17:43 
GeneralMy vote of 5 Pinmemberhitesh140715-Nov-11 12:52 
QuestionHere's how I used it for my ASP.NET Website PinmemberMember 818886925-Aug-11 7:27 
GeneralGreat article! Pinmemberminhhieu_dotnet11-Nov-10 23:21 
QuestionC# and watermarking? Pinmemberlapadets27-Sep-10 8:26 
AnswerRe: C# and watermarking? PinmemberMember 813589222-Aug-11 3:38 
QuestionHow can apply watermarks to a printer? Pinmembergiji gangadharan10-Mar-10 20:48 
GeneralGreat Work Pinmemberranunes17-Sep-09 4:28 
QuestionGreat work! How to watermark diagonally? PinmemberDesignerInCore9-Sep-09 17:54 
AnswerRe: Great work! How to watermark diagonally? Pinmemberalhambra-eidos17-Aug-10 7:28 
GeneralBrilliant Pinmembertezza9812-May-09 16:23 
Generalsetlogo as vedio watermarking at center over vedio Pinmembersushilabhanvar15-Apr-09 2:29 
Generalrequired c# Vedio watermarking code Pinmembersushilabhanvar7-Apr-09 1:00 
Question? Pinmemberbatlai13-Mar-09 4:42 
Questionvideo watermarking PinmemberMember 182483018-Feb-09 21:23 
GeneralThank you! PinmemberEffice16-Dec-08 4:45 
JokeThanks Pinmemberbgs2644-Sep-08 5:04 
Questionhow can i extracted the watermake ? Pinmemberwxk56613-Aug-08 18:25 
AnswerRe: how can i extracted the watermake ? Pinmembermatt332-Sep-08 23:05 
QuestionAnimated Gifs?? PinmemberJoshKrak3-Aug-08 17:15 
GeneralResize the image Pinmembermatt3316-Jun-08 23:53 
Generalthanks, it's powerfull and simple PinmemberM.S. Babaei15-Nov-07 19:18 
GeneralThe best page examplke for watermarking in GDI+ Pinmemberkesonek19-Mar-07 11:12 
QuestionHow can Increase/Decrease the watermark transparency? PinmemberCamilo Vergara M.5-Oct-06 3:44 
AnswerRe: How can Increase/Decrease the watermark transparency? PinmemberCardinal427-May-07 6:17 
GeneralReplace color doesn't seem to work on jpg Pinmembersivrat1017-Aug-06 22:10 
GeneralA generic error occurred in GDI+ [modified] PinmemberAmethi29-May-06 5:13 
GeneralRe: A generic error occurred in GDI+ [modified] PinmemberAmethi29-May-06 6:22 
QuestionMore detail on the opacity #'s? PinmemberMorningZ20-Feb-06 17:33 
AnswerRe: More detail on the opacity #'s? Pinmembertoffee44224-May-06 17:04 
Generalimage quality loss PinmemberKelraad13-Nov-05 4:13 
GeneralRe: image quality loss PinmemberShinchi19-Apr-06 1:35 
GeneralThe dynamic GIF goes to static GIF. PinmemberLittleTiger3-Aug-05 21:59 
GeneralAlternative to Alpha Manipulation PinsussMark Miller4-Dec-03 14:38 
GeneralRe: Alternative to Alpha Manipulation Pinmembersides_dale1-Jul-05 2:14 
GeneralThis is cool stuff PinsussAnonymous27-Sep-02 12:30 
GeneralRe: This is cool stuff PinmemberAnthony_Yio16-Apr-04 2:21 
Generalwatermark ??! PinmemberMario M.24-Sep-02 5:10 
GeneralRe: watermark ??! PinmemberTim Lesher24-Sep-02 7:12 
GeneralRe: watermark ??! PinmemberStephaneRodriguez26-Sep-02 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150123.1 | Last Updated 26 Sep 2002
Article Copyright 2002 by Joel Neubeck
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid