Image Watermark and text watermark

Source: Internet
Author: User

 

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();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.