Sunday, June 21, 2015

Add image (logo) watermark to images dynamically while uploading in

Introduction: In this article I will explain how we can add the watermark of logo to images while uploading in (C#, VB)
Add image (logo) watermark to images dynamically while uploading in

To implement this functionality to project/website add a webform. Create a folder images in project/website to store the uploaded images. Now copy the logo of website and keep/paste it in images folder also. Drag and drop the fileupload control from toolbox to aspx page.

HTML markup of page:
   <fieldset style="width:500px">
    <legend>Example to add Watermark of Logo to uploaded images</legend>
    <tr><td>Upload File</td><td>
        <asp:FileUpload ID="FileUpload1" runat="server" /></td></tr>
        <asp:Button ID="Button1" runat="server" Text="Upload File" /></td></tr>

Now add the namespaces.
using System.IO;
using System.Drawing;

Imports System.IO
Imports System.Drawing

After that write the below given code on button click.
protected void Button1_Click(object sender, EventArgs e)
  Bitmap bmpStamp = new Bitmap(Server.MapPath("images") + "\\" + "logo.png");
            Bitmap bmpUpload = new Bitmap(FileUpload1.PostedFile.InputStream, false);
            Graphics graphicsObj = Graphics.FromImage(bmpUpload);
            Point postionWaterMark = new Point((bmpUpload.Width / 20), (bmpUpload.Height / 20));
            graphicsObj.DrawImage(bmpStamp, postionWaterMark);
            string filepath = Server.MapPath("~/images/") + Guid.NewGuid() + Path.GetFileName(FileUpload1.PostedFile.FileName) + ".jpg";
            Response.Write("<script type=\"text/javascript\">alert('image uploaded Successfully!!!');</script>"); 
        catch (Exception ex)

  Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim bmpStamp As New Bitmap(Server.MapPath("images") + "\" + "logo.png")
            Dim bmpUpload As New Bitmap(FileUpload1.PostedFile.InputStream, False)
            Dim graphicsObj As Graphics = Graphics.FromImage(bmpUpload)
            Dim postionWaterMark As New Point((bmpUpload.Width / 20), (bmpUpload.Height / 20))
            graphicsObj.DrawImage(bmpStamp, postionWaterMark)
            Dim filepath As String = Server.MapPath("~/images/") & Guid.NewGuid().ToString() & Path.GetFileName(FileUpload1.PostedFile.FileName) + ".jpg"
            Response.Write("<script type=""text/javascript"">alert('image uploaded Successfully!!!');</script>")
        Catch ex As Exception 
        End Try
    End Sub

Now build, run the project and see the result.

In this article we have learn to add logo watermark to images dynamically while uploading in (C#,VB). I hope you enjoyed this article. 


  1. That's pretty darn sweet! Can you add the watermark to an existing image?