Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/089c2d464f35ca1060569bbeb2d353d7 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/089c2d464f35ca1060569bbeb2d353d7 to your computer and use it in GitHub Desktop.

Aspose.Imaging .NET API allows to easily add watermark or signature on your images or photos in your .NET application or Web service.

You can:

  • Add watermark to image;
  • Add signature to image.

Interested ?

You may go further at : https://products.aspose.com/imaging/net/

using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing JPG image
using (Image image = Image.Load(dataDir + "template.tiff"))
{
// 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(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Create an instance of Image and load the primary image
using (Image canvas = Image.Load(dataDir + "template.tiff"))
{
// Create another instance of Image and load the secondary image containing the signature graphics
using (Image signature = Image.Load(dataDir + "template.gif"))
{
// Create an instance of Graphics class and initialize it using the object of the primary image
Graphics graphics = new Graphics(canvas);
// Call the DrawImage method while passing the instance of secondary image and appropriate location. The following snippet tries to draw the secondary image at the right bottom of the primary image
graphics.DrawImage(signature, new Point(canvas.Height - signature.Height, canvas.Width - signature.Width));
canvas.Save(dataDir + "result.png", new PngOptions());
}
}
File.Delete(dataDir + "result.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment