Friday, June 19, 2015

Add watermark text to images dynamically while uploading in

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

To implement this functionality to project/website add a webform. Create a folder images in project/website to store the uploaded images. Drag and drop the fileupload control from toolbox to aspx page.

HTML markup of page:
<fieldset style="width:500px">
    <legend>Example to add Watermark 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 bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false);
            Graphics graphicsobj = Graphics.FromImage(bmp);
            Brush brush = new SolidBrush(Color.FromArgb(80, 255, 255, 255));
            Point postionWaterMark = new Point((bmp.Width / 2), (bmp.Height * 9 / 10));
            graphicsobj.DrawString("©", new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel), brush, postionWaterMark);
            string filepath = Server.MapPath("~/images/") + Guid.NewGuid() + Path.GetFileName(FileUpload1.PostedFile.FileName);
            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 bmp As New Bitmap(FileUpload1.PostedFile.InputStream, False)
            Dim graphicsobj As Graphics = Graphics.FromImage(bmp)
            Dim brush As Brush = New SolidBrush(Color.FromArgb(80, 255, 255, 255))
            Dim postionWaterMark As New Point((bmp.Width / 2), (bmp.Height * 9 / 10))
            graphicsobj.DrawString("©", New System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel), brush, postionWaterMark)
            Dim filepath As String = Server.MapPath("~/images/") & Guid.NewGuid().ToString() & Path.GetFileName(FileUpload1.PostedFile.FileName)
            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.
Add watermark text to images dynamically while uploading in


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

No comments:

Post a Comment