Draw text watermark on thumbnail image
View as MarkdownThis sample shows how to create a thumbnail image and draw a semi-transparent text watermark on top of it using the GraphicsMill transforms and advanced drawing APIs. It demonstrates how to resize the source image with the Resize transform, register a custom font, and render text with Drawer and PlainText. You can read more information about drawing in the documentation.
Code Snippet
// Read the source image and prepare pipeline elements.
using (var reader = ImageReader.Create("Venice.jpg"))
using (var resize = new Resize(500, 500, ResizeInterpolationMode.Anisotropic9, ResizeMode.Fit))
using (var drawer = new Drawer())
using (var writer = new JpegWriter("TextWatermark.jpg"))
using (var fr = new CustomFontRegistry())
{
// Add font to the custom font registry and get its PostScript name.
var psName = fr.Add("Jost-Regular.ttf")[0].PostscriptName;
// Configure the Drawer to render the watermark text.
drawer.Draw += (sender, e) =>
{
// Set the custom font registry to the current graphics context.
e.Graphics.FontRegistry = fr;
// Create the font to be used for the watermark text.
var font = e.Graphics.CreateFont(psName, 50);
// Create the text object with semi-transparent white brush.
var text = new PlainText("WATERMARK TEXT", font)
{
Brush = new SolidBrush(RgbColor.White.ScaleAlpha(0.7f)),
Alignment = TextAlignment.Left,
};
// Place the text with a small margin from the left/bottom edges.
var margin = 10;
text.Position = new System.Drawing.PointF(margin, e.Height - margin);
// Draw the watermark text onto the image.
e.Graphics.DrawText(text);
};
// Run the pipeline.
Pipeline.Run(reader + resize + drawer + writer);
}
Input
Venice.jpg
Output
TextWatermark.jpg
Used APIs
- Aurigma.GraphicsMill.Codecs.ImageReader
- Aurigma.GraphicsMill.Codecs.ImageReader.Create(String)
- Aurigma.GraphicsMill.Codecs.JpegWriter
- Aurigma.GraphicsMill.Color.ScaleAlpha(Single)
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry
- Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Add(String)
- Aurigma.GraphicsMill.Drawing.Drawer
- Aurigma.GraphicsMill.Drawing.DrawEventArgs.Graphics
- Aurigma.GraphicsMill.Drawing.FontAddResult.PostscriptName
- Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.DrawText(Text)
- Aurigma.GraphicsMill.Drawing.Graphics.FontRegistry
- Aurigma.GraphicsMill.Drawing.PlainText
- Aurigma.GraphicsMill.Drawing.PlainText.Alignment
- Aurigma.GraphicsMill.Drawing.PlainText.Position
- Aurigma.GraphicsMill.Drawing.SolidBrush
- Aurigma.GraphicsMill.Drawing.Text.Brush
- Aurigma.GraphicsMill.Drawing.TextAlignment
- Aurigma.GraphicsMill.Transforms.Resize
- Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode
- Aurigma.GraphicsMill.Transforms.ResizeMode