/// <summary> /// Add text watermark over the image. /// </summary> /// <param name="img"></param> /// <param name="text">text to draw over the image</param> /// <param name="ops">Text watermark options <see cref="TextWatermarkOptions"/></param> public static Image AddTextWatermark(this Image img, string text, TextWatermarkOptions ops) { using (var graphics = Graphics.FromImage(img)) { var bgPos = TextWatermarkPosition.SetBGPos(img.Width, img.Height, ops.FontSize, ops.Location, ops.Margin); var sf = new StringFormat() { FormatFlags = StringFormatFlags.NoWrap }; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; //graphics.TextRenderingHint = TextRenderingHint.AntiAlias; //graphics.TextContrast = 12; //graphics.CompositingMode = CompositingMode.SourceOver; //graphics.InterpolationMode = InterpolationMode.High; // Draw background if not fully transparent if (ops.BGColor.A > 0) { var bgBrush = new SolidBrush(ops.BGColor); graphics.FillRectangle(bgBrush, bgPos); } // Set font to use var ff = new FontFamily(ops.FontName); var font = new Font(ff, ops.FontSize, ops.FontStyle, GraphicsUnit.Pixel); // Measure text size var textMetrics = graphics.MeasureString(text, font, img.Width, sf); var beforeText = TextWatermarkPosition.SetTextAlign(textMetrics, img.Width, ops.Location); var drawPoint = new PointF(beforeText, bgPos.Y + (bgPos.Height / 4)); var outlineBrush = new SolidBrush(ops.OutlineColor); using (var pen = new Pen(outlineBrush, ops.OutlineWidth)) { using (var p = new GraphicsPath()) { p.AddString(text, ff, (int)ops.FontStyle, ops.FontSize, drawPoint, sf); // Draw text outline if not fully transparent if (ops.OutlineColor.A > 0) { graphics.DrawPath(pen, p); } // Draw text if not fully transparent if (ops.TextColor.A > 0) { var textBrush = new SolidBrush(ops.TextColor); graphics.FillPath(textBrush, p); } } } } return(img); }
public static void TextWatermark(this Image img, string text, string color = "#77FFFFFF", string bgColor = "#00000000", string fontFamily = "Arial", int size = 24, TargetSpot spot = TargetSpot.BottomLeft, FontStyle style = FontStyle.Regular, int margin = 10) { var graphics = Graphics.FromImage(img); graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.TextContrast = 12; graphics.SmoothingMode = SmoothingMode.None; graphics.CompositingMode = CompositingMode.SourceOver; var _bgAlpha = int.Parse(bgColor.Substring(1, 2), NumberStyles.HexNumber); var _bgColor = bgColor.Substring(3, 6); var bgBrush = new SolidBrush( Color.FromArgb(_bgAlpha, ColorTranslator.FromHtml($"#{_bgColor}"))); var rectPos = TextWatermarkPosition.SetBGPos(img.Width, img.Height, size, spot, margin); graphics.FillRectangle(bgBrush, rectPos); var textFont = new Font(fontFamily, size, style, GraphicsUnit.Pixel); var _alpha = int.Parse(color.Substring(1, 2), NumberStyles.HexNumber); var _color = color.Substring(3, 6); var textBrush = new SolidBrush( Color.FromArgb(_alpha, ColorTranslator.FromHtml($"#{_color}"))); var textMetrics = graphics.MeasureString(text, textFont); var beforeText = TextWatermarkPosition.SetTextAlign(textMetrics, img.Width, spot); var drawPoint = new PointF(beforeText, rectPos.Y + (rectPos.Height / 4)); graphics.DrawString(text, textFont, textBrush, drawPoint); graphics.Dispose(); }