In this article I will explain how to add Watermark text to Images / Photos dynamically on the fly in ASP.Net using C# and VB.Net.
The Watermark text will be dynamically created on the Image / Photo with the help of Graphics Library of .Net.
HTML Markup
The HTML Markup consists of an ASP.Net FileUpload control and a Button to upload the image.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" Text="Upload" runat="server" OnClick="Upload" />
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
VB.Net
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Creating / Adding Watermark Text to Image in ASP.Net
The uploaded image is first read into a Bitmap object. Then using the Graphics class the Font, Font Size and the Color of the Watermark text is set.
Now the Watermark Text is ready to be drawn on the Image, but in order to place it on the right bottom corner we need to determine the height and the width of the Watermark Text.
Using the height and the width of the Watermark Text, the X-Y coordinate is calibrated and the Watermark Text is drawn on the image.
Finally the Bitmap is saved into a MemoryStream object which later written to the Response Stream so that the Watermarked image is sent for download.
C#
protected void Upload(object sender, EventArgs e)
{
string watermarkText = "© ASPSnippets.com";
//Get the file name.
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";
//Read the File into a Bitmap.
using (Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false))
{
using (Graphics grp = Graphics.FromImage(bmp))
{
//Set the Color of the Watermark text.
Brush brush = new SolidBrush(Color.Red);
//Set the Font and its size.
Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
//Determine the size of the Watermark text.
SizeF textSize = new SizeF();
textSize = grp.MeasureString(watermarkText, font);
//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)));
grp.DrawString(watermarkText, font, brush, position);
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Watermarked image to the MemoryStream.
bmp.Save(memoryStream, ImageFormat.Png);
memoryStream.Position = 0;
//Start file download.
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
//Write the MemoryStream to the Response.
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
}
}
}
VB.Net
Protected Sub Upload(sender As Object, e As EventArgs)
Dim watermarkText As String = "© ASPSnippets.com"
'Get the file name.
Dim fileName As String = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png"
'Read the File into a Bitmap.
Using bmp As New Bitmap(FileUpload1.PostedFile.InputStream, False)
Using grp As Graphics = Graphics.FromImage(bmp)
'Set the Color of the Watermark text.
Dim brush As Brush = New SolidBrush(Color.Red)
'Set the Font and its size.
Dim font As Font = New System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel)
'Determine the size of the Watermark text.
Dim textSize As New SizeF()
textSize = grp.MeasureString(watermarkText, font)
'Position the text and draw it on the image.
Dim position As New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 10)))
grp.DrawString(watermarkText, font, brush, position)
Using memoryStream As New MemoryStream()
'Save the Watermarked image to the MemoryStream.
bmp.Save(memoryStream, ImageFormat.Png)
memoryStream.Position = 0
'Start file download.
Response.Clear()
Response.ContentType = "image/png"
Response.AddHeader("Content-Disposition", Convert.ToString("attachment; filename=") & fileName)
'Write the MemoryStream to the Response.
memoryStream.WriteTo(Response.OutputStream)
Response.Flush()
Response.Close()
Response.[End]()
End Using
End Using
End Using
End Sub
Screenshot
Demo
Downloads