using System; using System.Drawing; using System.Drawing.Imaging; using System.Web; using System.IO;
// Draw the image in the bottom-right corner 图片右下角 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 corner using (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(); }
水印在图片中心 这个图只截取了一部分
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(); }