using System;using System.Drawing;using System.Drawing.Imaging;using System.Web;using System.IO;
// Draw the image in the bottom-right corner Image Using (system. Drawing. Image Watermark = system. Drawing. image. fromfile (httpcontext. Current. server. mappath ("~ /GFX/watermark.png ") {using (Graphics G = graphics. fromimage (ARGs. image) {// setup the quality settings (max) g. pixeloffsetmode = system. drawing. drawing2d. pixeloffsetmode. highquality; G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; G. interpolationmode = system. drawing. drawing2d. interpolationmode. highqualitybicubic; G. compositingquality = system. drawing. drawing2d. compositingquality. highquality; // draw the image G. drawimageunscaled (watermark, argS. image. width-watermark. width, argS. image. height-watermark. height, watermark. width, watermark. height); G. dispose ();}}
// Draw the text in the bottom-right cornerusing (Graphics g = Graphics.FromImage(args.Image)){ string text = "MyLogo"; Font font = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold); SizeF stringSize = g.MeasureString(text, font); PointF location = new PointF(args.Image.Width - stringSize.Width - 10, args.Image.Height - stringSize.Height - 10); // Setup the quality settings g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // Draw the text g.DrawString(text, font, Brushes.Red, location); g.Dispose();}
Watermark in the image center. only part of the image is captured.
using (Graphics g = Graphics.FromImage(args.Image)){ // Draw a horizontal text in the image center string text = "My NEW watermark"; Font font = new Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold); SizeF stringSize = g.MeasureString(text, font); PointF location = new PointF((args.Image.Width - stringSize.Width) / 2, (args.Image.Height - stringSize.Height) / 2); // Setup the quality settings g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // Draw the text g.DrawString(text, font, new SolidBrush(Color.FromArgb(85, 255, 255, 80)), location); // Draw a vertical text in the bottom-right corner DateTime dt = DateTime.UtcNow; text = "My watermark - " + dt.ToString("s"); font = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold); StringFormat stringFormat = new StringFormat(); stringFormat.FormatFlags = StringFormatFlags.DirectionVertical; stringSize = g.MeasureString(text, font, 100, stringFormat); location = new PointF(args.Image.Width - stringSize.Width - 5, args.Image.Height - stringSize.Height - 5); // Draw another text g.DrawString(text, font, new SolidBrush(Color.FromArgb(80, 255, 255, 255)), location, stringFormat); g.Dispose();}