article

How to Add Watermark to an Image inside .NET applications

Email
Submitted on: 12/30/2015 9:09:56 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3190
 
     This technical tip explains how .NET developers can add a watermark to an image inside their .NET applications. This document explains how to add a watermark to an image using Aspose.Imaging. Adding a watermark to an image is a common requirement for image processing applications. This example uses the Graphics class to draw a string on the image surface. To demonstrate the operation, we will load a BMP image from disk and draw a string as the watermark on the image surface using the Graphics class' DrawString method. We'll save the image to PNG format using the PngOptions class.

 
				This technical tip explains how .NET developers can add a watermark to an image inside their .NET applications. This document explains how to add a watermark to an image using Aspose.Imaging. Adding a watermark to an image is a common requirement for image processing applications. This example uses the Graphics class to draw a string on the image surface. To demonstrate the operation, we will load a BMP image from disk and draw a string as the watermark on the image surface using the Graphics class' DrawString method. We'll save the image to PNG format using the PngOptions class. Below is a code example that demonstrates how to add a watermark to an image. The example source code has been split into parts to make it easy to follow. Step by step, the examples show how to:
•	Load an image.
•	Create and initialize a Graphics object.
•	Create and initialize Font and SolidBrush objects.
•	Draw a string as watermark using the Graphics class' DrawString method.
•	Save image to PNG. 
// Adding a Watermark
//[C# Code Sample]
//Load an existing JPG image
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(myDir + "sample.jpg"))
{
//Declare a String object with Watermark Text
String theString = "45 Degree Rotated Text";
//Create and initialize an instance of Graphics class
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);
//Initialize an object of SizeF to store image Size
Aspose.Imaging.SizeF sz = graphics.Image.Size;
//Creates an instance of Font, initialize it with Font Face, Size and Style
Aspose.Imaging.Font font = new Aspose.Imaging.Font("Times New Roman", 20, FontStyle.Bold);
//Create an instance of SolidBrush and set its various properties
Aspose.Imaging.Brushes.SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush();
brush.Color = Aspose.Imaging.Color.Red;
brush.Opacity = 0;
//Initialize an object of StringFormat class and set its various properties
Aspose.Imaging.StringFormat format = new Aspose.Imaging.StringFormat();
format.Alignment = Aspose.Imaging.StringAlignment.Center;
format.FormatFlags = Aspose.Imaging.StringFormatFlags.MeasureTrailingSpaces;
//Create an object of Matrix class for transformation
Aspose.Imaging.Matrix matrix = new Aspose.Imaging.Matrix();
//First a translation
matrix.Translate(sz.Width / 2, sz.Height / 2);
//Then a rotation
matrix.Rotate(-45.0f);
//Set the Transformation through Matrix
graphics.Transform = matrix;
//Draw the string on Image
graphics.DrawString(theString, font, brush, 0, 0, format);
//Save output to disk
image.Save(myDir + "output.jpg");
}
 
//[VB.NET Code Sample]
'Load an existing JPG image
Using image As Aspose.Imaging.Image = Aspose.Imaging.Image.Load(myDir & "sample.jpg")
	'Declare a String object with Watermark Text
	Dim theString As String = "45 Degree Rotated Text"
	'Create and initialize an instance of Graphics class
	Dim graphics As New Aspose.Imaging.Graphics(image)
	'Initialize an object of SizeF to store image Size
	Dim sz As Aspose.Imaging.SizeF = graphics.Image.Size
	'Creates an instance of Font, initialize it with Font Face, Size and Style
	Dim font As New Aspose.Imaging.Font("Times New Roman", 20, FontStyle.Bold)
	'Create an instance of SolidBrush and set its various properties
	Dim brush As New Aspose.Imaging.Brushes.SolidBrush()
	brush.Color = Aspose.Imaging.Color.Red
	brush.Opacity = 0
	'Initialize an object of StringFormat class and set its various properties
	Dim format As New Aspose.Imaging.StringFormat()
	format.Alignment = Aspose.Imaging.StringAlignment.Center
	format.FormatFlags = Aspose.Imaging.StringFormatFlags.MeasureTrailingSpaces
	'Create an object of Matrix class for transformation
	Dim matrix As New Aspose.Imaging.Matrix()
	'First a translation
	matrix.Translate(sz.Width / 2, sz.Height / 2)
	'Then a rotation
	matrix.Rotate(-45.0f)
	'Set the Transformation through Matrix
	graphics.Transform = matrix
	'Draw the string on Image
	graphics.DrawString(theString, font, brush, 0, 0, format)
	'Save output to disk
	image.Save(myDir & "output.jpg")
End Using
More about Aspose.Imaging for .NET


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.