programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Website by URL, How to Play YouTube Videos in by using Jquery , Jquery Change div background on mouseover in , JQuery UI Datepicker (Calendar) etc.

Thursday, October 9, 2014 Watermark Text on uploaded Image

Add Watermark Text on uploaded Image in

In this Post I will explain how to add Watermark text in to uploadImages in C# code.
The Watermark will be dynamically (At run time) created on the uploaded Images with the help of Graphics Library of .Net. if you want to Learn from starting then go to introduction of post.

NameSpace For add watermark in Image:

Given Fallowing Name space use for adding watermark in Image.

using System.IO;
using System.Drawing;
using System.Drawing.Imaging; 

How to create watermark in Image:

  • The uploaded image is first read into a Bitmap object.

Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false)

  • Then using the Graphics class the Font, Font Size and the Color of the Watermark text is set.

Graphics grp = Graphics.FromImage(bmp)       
            //Set the Color of the Watermark text.
            Brush brush = new SolidBrush(Color.Red);//Color which you want. 
            //Set the Font and its size.
            Font font = new System.Drawing.Font("Font name", size, FontStyle.Font style, GraphicsUnit.Pixel); 
            //Determine the size of the Watermark text.
            SizeF textSize = new SizeF();//get text size.
            textSize = grp.MeasureString(watermarkText, font);

  • We need to determine the height and the width of the Watermark Text.
  • Position the text and draw it on the image.

  Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));

  • Now the Watermark Text is ready to be drawn on the Image.

grp.DrawString(watermarkText, font, brush, position);

Adding watermark text or picture in Image in

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Copy Right Image by C# code</title>
    <style type="text/css">
            text-decoration: underline;
            color: #990000;
            font-family: Arial, Helvetica, sans-serif;
            color: #0000FF;
    <form id="form1" runat="server">
    <div align="center" style="height: 226px">
    <h1 class="style1">Create Copy Right Image by C# code. </h1>
    <p class="style2"> Upload Image And Insert Water Mark in uploaded Image </p>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:Button Text="Upload" runat="server" OnClick="Upload" />
        <br />
        This is a Demo Application form tutorial. if you want more the visit
        <br />
        <a href="" target="_blank">

C# Code for Automatically add watermark in Image:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public partial class CS : System.Web.UI.Page
    protected void Pageload()

    protected void Upload(object sender, EventArgs e)
        string watermarkText = "© Prigramming with C#";
        string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";
        using (Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false))
            using (Graphics grp = Graphics.FromImage(bmp))
                Brush brush = new SolidBrush(Color.Red);
                Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
                SizeF textSize = new SizeF();
                textSize = grp.MeasureString(watermarkText, font);
                Point position = new Point((bmp.Width - ((int)textSize.Width + 20)), (bmp.Height - ((int)textSize.Height + 10)));
                grp.DrawString(watermarkText, font, brush, position);
                using (MemoryStream memoryStream = new MemoryStream())
                    bmp.Save(memoryStream, ImageFormat.Png);
                    memoryStream.Position = 0;
                    Response.ContentType = "image/png";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
} - adding watermark text images uploading Time By code: Watermark Text on uploaded ImagebyParijat Watermark Text Related Other Post:

No comments:

Post a Comment