-->

How to add Watermark Text to an image dynamically while uploading in ASP.NET.

Introduction

In this post, I explain how to add Watermark Text to an image dynamically while uploading in ASP.NET.

Steps :

Step-1 : Create New Project

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add Web Page and Design for Upload Image with Watermark Text.

Go to Solution Explorer > Right Click on Project under solution explorer > Add > New item > Select Web Form using Master Page under web > Enter Name > > Select Master Page>Add.

HTML Code
     <h3>Upload Image with Watermark Text</h3>
    <table>
        <tr>
            <td>Select Image : </td>
            <td>
                <asp:FileUpload ID="FU1" runat="server" /></td>

        </tr>
        <tr>
            <td>Watermark Text : </td>
            <td>
                <asp:TextBox ID="txtWatermarkText" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:Button ID="btnSave" runat="server" Text="Upload Image" OnClick="btnSave_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Label ID="lblMsg" runat="server" ForeColor="Red" />
            </td>
        </tr>
    </table>
    <asp:Image ID="Image1" runat="server" />
        

Step-3: Add a Folder for Save uploaded Images.

Go to Solution Explorer > Right Click on Project under solution explorer > Add > New Folder > Enter Name.

Step-4: Write code for Upload Image with Watermark Text.

Write below code into button click event for Upload Image with Watermark Text.

         protected void btnSave_Click(object sender, EventArgs e)
        {
            // Here We will upload image with watermark Text
            string fileName = Guid.NewGuid() + Path.GetExtension(FU1.PostedFile.FileName);
            Image upImage = Image.FromStream(FU1.PostedFile.InputStream);
            using (Graphics g = Graphics.FromImage(upImage))
            {

                // For Transparent Watermark Text 
                int opacity = 128; // range from 0 to 255

                //SolidBrush brush = new SolidBrush(Color.Red);
                SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, Color.Red));
                Font font = new Font("Arial",16);
                g.DrawString(txtWatermarkText.Text.Trim(), font, brush, new PointF(0, 0));
                upImage.Save(Path.Combine(Server.MapPath("~/UploadFiles"),fileName));
                Image1.ImageUrl = "~/UploadFiles" + "//" + fileName;
            }
        }
        

STEP-5: Run Application

Run Application and Get result in your browser.
Thank You

Related Post:


Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.