Asp.net Watermark Text on uploaded Image

Add Watermark Text on uploaded Image in Asp.net:



In this Post I will explain how to add Watermark text in to uploadImages in asp.net 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 asp.net from starting then go to introduction of asp.net 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 asp.net:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Copy Right Image by C# code</title>
    <style type="text/css">
        .style1
        {
            text-decoration: underline;
            color: #990000;
            font-family: Arial, Helvetica, sans-serif;
        }
        .style2
        {
            color: #0000FF;
        }
    </style>
</head>
<body>
    <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 asp.net tutorial. if you want more the visit
        <br />
        <a href="http://asp-net-by-parijat.blogspot.in" target="_blank">
        http://asp-net-by-parijat.blogspot.in
        </a>
        </div>
    </form>
</body>
</html> 


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 = "©Asp.net 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.Clear();
                    Response.ContentType = "image/png";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    memoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.Close();
                    Response.End();
                }
            }
        }
    }
}

Asp.net - adding watermark text images uploading Time By code:

Asp.net Watermark Text on uploaded ImagebyParijat
Asp.net Watermark Text

Asp.net Related Other Post:

Comments

Popular posts from this blog