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 |
Asp.net Related Other Post:
- SQL Server questions for interview
- How do you do html text encodes using JavaScript
- How to calculate first date and last date of month
- How to Calculate Number of Sunday in a particular month
- How to get Asp.net the First Sunday of the Given Month
- Displaying the textbox value in javascript Messagebox
- Limit Number of Characters in a TextArea using jQuery
- jquery disable or Enable submit button after validation
- What is ASP.NET FRAMEWORK PART 1 Programming
- Display selected Date from data base into asp.net calendar
- How to create Line chart:,How to Make Data Table By C# code:
- Example of C# for Bind Data to asp.net Textbox inside gridview control
- Bind Data to asp.net textbox control in inside of gridview Using C# Example
- Example of Crystal report(Crystal_report_in asp.net programming )
- SQL Helper Class, Example of how to add captcha in Asp.net using C#
- Sql Query for asp.net Programming , How to create graph in asp.net:
- How to use CAPTCHA in asp.net,How to use Asp.net regular expression
- How to Achieve Many Selection in Drop Down List
- Create data Table Dynamically and bind to Drop down List in asp.net
- Sending mail from web application using google Mail ID
- Display selected Date from data base into asp.net calendar
- How to Make a HTML Table by C# code in asp.net programming
Comments
Post a Comment