Last active
June 8, 2022 07:01
-
-
Save aspose-com-gists/940c35e16a1b6065fafce68838d36c63 to your computer and use it in GitHub Desktop.
Add Watermark to Image in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load an existing PNG image | |
using (Image image = Image.Load("Image.png")) | |
{ | |
// Declare a String object with Watermark Text | |
string theString = "45 Degree Rotated Text"; | |
// Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size | |
Graphics graphics = new Graphics(image); | |
SizeF sz = graphics.Image.Size; | |
// Creates an instance of Font, initialize it with Font Face, Size and Style | |
Font font = new Font("Times New Roman", 20, FontStyle.Bold); | |
// Create an instance of SolidBrush and set its various properties | |
SolidBrush brush = new SolidBrush(); | |
brush.Color = Color.Red; | |
brush.Opacity = 0; | |
// Initialize an object of StringFormat class and set its various properties | |
StringFormat format = new StringFormat(); | |
format.Alignment = StringAlignment.Center; | |
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; | |
// Create an object of Matrix class for transformation | |
Matrix matrix = new Matrix(); | |
// First a translation then a rotation | |
matrix.Translate(sz.Width / 2, sz.Height / 2); | |
matrix.Rotate(-45.0f); | |
// Set the Transformation through Matrix | |
graphics.Transform = matrix; | |
// Draw the string on Image Save output to disk | |
graphics.DrawString(theString, font, brush, 0, 0, format); | |
image.Save("watermarked.png"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load an existing PNG image | |
using (Image image = Image.Load("image.png")) | |
{ | |
// Declare a String object with Watermark Text | |
string theString = "This is watermark"; | |
// Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size | |
Graphics graphics = new Graphics(image); | |
SizeF sz = graphics.Image.Size; | |
// Creates an instance of Font, initialize it with Font Face, Size and Style | |
Font font = new Font("Times New Roman", 20, FontStyle.Bold); | |
// Create an instance of SolidBrush and set its various properties | |
SolidBrush brush = new SolidBrush(); | |
brush.Color = Color.Red; | |
brush.Opacity = 0; | |
// Initialize an object of StringFormat class and set its various properties | |
StringFormat format = new StringFormat(); | |
format.Alignment = StringAlignment.Center; | |
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; | |
// Draw the string on Image Save output to disk | |
graphics.DrawString(theString, font, brush, 0, 0, format); | |
image.Save("watermarked.jpg"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment