BlueLemonCode.Com

Yet Another Tech Blog

Adding watermark text or picture in Image

Introduction

In professional sites/tools, we often see that the copyright images are stamped with some text/images so as to prevent others from using it or at least to warn that these are the proprietary images and one should not simple copy and use it for some other purpose.

Though, a Photoshop guy can do it in few minutes its better if we can do it in some automated way. This is required if one have thousands of images and of course for someone who dosent know how to use photoshop well... like me :)

In .Net framework, classes in System.Drawing namespace make it really easy.

Article Body

There is nothing much to explain so we will create a demo project (as usual). We will create a page from where a image can be uploaded and upon clicking the button, the watermark text or stamp image will be added in our uploaded image. I have kept it very simple. The focus is on to demonstrate how to add watermark.

After adding file upload control and a button, this is the code which goes inside button click event. For using this code, you need to import

System.Drawing namespace

This is to add watermark text on uploaded image

C#

protected void btnAddWatermark_Click(object sender, EventArgs e)
    {

        //Create bitmap object from uploaded file
        Bitmap bmpUpload = new Bitmap(upld.PostedFile.InputStream, false);
        //Create object of Graphics class using uploaded files bitmap
        Graphics graphicsObj = Graphics.FromImage(bmpUpload);
        //Create Brush class's object and define color of text to be added in image
        Brush brush = new SolidBrush(Color.Gray);
        //define position of image or text to be added for watermarking purpose in original uploaded image
        //You could have difference logic to device watermark position within original image. Based on where watermark should be place
        //like at top/bottom/center of image etc.
        Point postionWaterMark = new Point((bmpUpload.Width/10) , (bmpUpload.Height/10));
        graphicsObj.DrawString("© Copyright BlueLemonCode.com", new System.Drawing.Font("verdana", 50, FontStyle.Bold, GraphicsUnit.Pixel), brush, postionWaterMark);
        //save image in local folder. it can also be sent to client for download
        bmpUpload.Save(Server.MapPath("Images") + "\\" + System.IO.Path.GetFileName(upld.PostedFile.FileName) + ".jpg");
    }


For testing purpose, I have used one of the Windows standard wallpaper image (the copyright text is added for test purpose). After adding watermark text into image, this is how it looks

 

Sometimes, adding simple text is not enough. We can also add another small image which looks as a stamp on original image. To do that, we need to change the above code a little. Something like this

This is to add watermark image on uploaded image

protected void btnAddWatermark_Click(object sender, EventArgs e)
    {
        //Create bitmap object from copyright stamp image
        Bitmap bmpStamp = new Bitmap(Server.MapPath("Images") + "\\" + "copyright.png");
        //Create bitmap object from uploaded file
        Bitmap bmpUpload = new Bitmap(upld.PostedFile.InputStream, false);
        //Create object of Graphics class using uploaded files bitmap
        Graphics graphicsObj = Graphics.FromImage(bmpUpload);
        //define position of image or text to be added for watermarking purpose in original uploaded image
        //You could have difference logic to device watermark position within original image. Based on where watermark should be place
        //like at top/bottom/center of image etc.
        Point postionWaterMark = new Point((bmpUpload.Width/10) , (bmpUpload.Height/10));
        graphicsObj.DrawImage(bmpStamp, postionWaterMark);
        bmpUpload.Save(Server.MapPath("Images") + "\\" + System.IO.Path.GetFileName(upld.PostedFile.FileName) + ".jpg");
    }

The main change in the code is, instead of Graphics.DrawSstring method, we have used Graphics.DrawImage. I have used this copyright stamp image. Its very easy to create this kind of transparent .png image using Photoshop (Your PhotoShop guy can help you create something like this :) )

After running the above code and using this copyright stamp image, our original image looks like this

 

The code can be used in Console/WinForm application as it is; where it can be used to add watermark in bulk of images at a time. I hope this can be useful to someone who is looking for this functionality.