Home > ASP.NET, C# > Add watermark to an image using C#

Add watermark to an image using C#

Below is the sample. I call it ShowImage.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
 
<script runat="server">
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["image"] == null) return;
        string WatermarkImg = 
              Server.MapPath(@"images\watermark.png");
        string CarImg = 
              Server.MapPath(Request.QueryString["image"]);
 
        Bitmap bmp = new Bitmap(CarImg);
 
        Graphics g = Graphics.FromImage(bmp);
 
        g.SmoothingMode = SmoothingMode.HighQuality;
 
        Bitmap bmpWM = (Bitmap)Bitmap.FromFile(WatermarkImg);
 
        g.DrawImage(bmpWM, new Point(bmp.Width-bmpWM.Width,
                    bmp.Height-bmpWM.Height) );
 
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        bmp.Dispose();
        bmpWM.Dispose();
        g.Flush();
    }
</script>

You can call this using:

<asp:Image ID="Image4" runat="server" ImageUrl
     ="~/ShowImage.aspx?image=images\cadillac_thumb.jpg" />
<img src="ShowImage.aspx?image=images\pagani.jpg" />
VN:F [1.9.18_1163]
Rating: 3.3/5 (7 votes cast)
Add watermark to an image using C#, 3.3 out of 5 based on 7 ratings

No related posts.

Categories: ASP.NET, C#
  • Prince

    it run but how to add water mark as text at this please reply at. prince@globalitpoint.com

    VA:F [1.9.18_1163]
    Rating: 0.0/5 (0 votes cast)
  • http://www.ninethsense.com/ Praveen V Nair

    Like DrawImage(), you have DrawString()

    VA:F [1.9.18_1163]
    Rating: 0.0/5 (0 votes cast)