public static void DrawRoundedRectangle(this Graphics g, Brush brush, Pen pen, Rectangle rect, float radius) { using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangleProper(rect, radius); if (brush != null) g.FillPath(brush, gp); if (pen != null) g.DrawPath(pen, gp); } }
public static Image RoundedCorners(Image img, int cornerRadius) { Bitmap bmp = img.CreateEmptyBitmap(); using (Graphics g = Graphics.FromImage(bmp)) using (img) { g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangleProper(new RectangleF(0, 0, img.Width, img.Height), cornerRadius); using (TextureBrush brush = new TextureBrush(img)) { g.FillPath(brush, gp); } } } return bmp; }
public override Image Apply(Image img) { if (string.IsNullOrEmpty(Text)) { return img; } using (Font textFont = TextFont) { if (textFont == null || textFont.Size < 1) { return img; } NameParser parser = new NameParser(NameParserType.Text); if (img != null) { parser.ImageWidth = img.Width; parser.ImageHeight = img.Height; } string parsedText = parser.Parse(Text); Size textSize = Helpers.MeasureText(parsedText, textFont); Size watermarkSize = new Size(textSize.Width + BackgroundPadding * 2, textSize.Height + BackgroundPadding * 2); Point watermarkPosition = Helpers.GetPosition(Placement, Offset, img.Size, watermarkSize); Rectangle watermarkRectangle = new Rectangle(watermarkPosition, watermarkSize); if (AutoHide && !new Rectangle(0, 0, img.Width, img.Height).Contains(watermarkRectangle)) { return img; } using (Graphics g = Graphics.FromImage(img)) { g.SetHighQuality(); using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangleProper(watermarkRectangle, CornerRadius); if (DrawBackground) { Brush backgroundBrush = null; try { if (UseGradient) { if (UseCustomGradient && Gradient != null && Gradient.IsValid) { backgroundBrush = new LinearGradientBrush(watermarkRectangle, Color.Transparent, Color.Transparent, Gradient.Type); ColorBlend colorBlend = new ColorBlend(); IEnumerable<GradientStop> gradient = Gradient.Colors.OrderBy(x => x.Location); colorBlend.Colors = gradient.Select(x => x.Color).ToArray(); colorBlend.Positions = gradient.Select(x => x.Location / 100).ToArray(); ((LinearGradientBrush)backgroundBrush).InterpolationColors = colorBlend; } else { backgroundBrush = new LinearGradientBrush(watermarkRectangle, BackgroundColor, BackgroundColor2, GradientType); } } else { backgroundBrush = new SolidBrush(BackgroundColor); } g.FillPath(backgroundBrush, gp); } finally { if (backgroundBrush != null) backgroundBrush.Dispose(); } } if (DrawBorder) { using (Pen borderPen = new Pen(BorderColor)) { g.DrawPath(borderPen, gp); } } } using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) { float centerX = watermarkRectangle.Width / 2f; float centerY = watermarkRectangle.Height / 2f; if (DrawTextShadow) { using (Brush textShadowBrush = new SolidBrush(TextShadowColor)) { g.DrawString(parsedText, textFont, textShadowBrush, watermarkRectangle.X + centerX + TextShadowOffset.X, watermarkRectangle.Y + centerY + TextShadowOffset.Y, sf); } } using (Brush textBrush = new SolidBrush(TextColor)) { g.DrawString(parsedText, textFont, textBrush, watermarkRectangle.X + centerX, watermarkRectangle.Y + centerY, sf); } } } } return img; }