Add Watermark image (logo) on Upload Image in asp.net c#

Many times we have seen olx, quikr and many classified websites that have watermark logo on their upload images so this article we learn how to add watermark image/logo on upload image and save in our folder.
Source code:-
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="watermarkonuploadimage.aspx.cs" Inherits="watermarkonuploadimage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <fieldset style="width: 350px">

            <legend>Add Watermark image(logo) on Upload Image  </legend>
           
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br /><br />
            <asp:Button ID="btnupload" Text="Upload" runat="server" OnClick="btnupload_Click" />
        </fieldset>

    </form>
</body>
</html>



Code behind(C#):-
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;


public partial class watermarkonuploadimage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnupload_Click(object sender, EventArgs e)
    {

        try
        {

            Bitmap bitmapimgpath = new Bitmap(Server.MapPath("images") + "\\" + "logo.jpg");
            Bitmap bitmapupload = new Bitmap(FileUpload1.PostedFile.InputStream, false);
            Graphics graphicsObj = Graphics.FromImage(bitmapimgpath);
            Point postionWaterMark = new Point((bitmapupload.Width / 20), (bitmapupload.Height / 20));
            graphicsObj.DrawImage(bitmapimgpath, postionWaterMark);
            string filepath = Server.MapPath("~/images/") + Path.GetFileName(FileUpload1.PostedFile.FileName) + ".jpg";
            bitmapupload.Save(filepath);
            Response.Write("<script type=\"text/javascript\">alert('image uploaded Successfully!!!');</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }


    }
}
Out-Put:-



Add Watermark image (logo) on Upload Image in asp.net c# Add Watermark image (logo) on Upload Image in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 10:03:00 AM Rating: 5

No comments:

Powered by Blogger.