/// <summary> /// Draws this slider on the specified graphics object. /// </summary> /// <param name="g">The graphics object used for drawing.</param> public void Draw(Graphics g) { if (!Visible || Width == 0) { return; } Rectangle bounds = GetBounds(); using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangle(bounds, RoundingRadius); using (LinearGradientBrush lgb = new LinearGradientBrush(bounds, Color.Lighter(.3F), Color.Darker(.3F), LinearGradientMode.ForwardDiagonal)) { g.FillPath(lgb, gp); using (Pen l = new Pen(Color.Darker(.2f), 2)) using (Pen r = new Pen(Color.Lighter(.2f), 2)) { if (IsLeft) { g.DrawLine(l, bounds.Right - 1, bounds.Top, bounds.Right - 1, bounds.Height); } else { g.DrawLine(r, bounds.Left + 1, bounds.Top, bounds.Left + 1, bounds.Right); } } } } }
/// <summary> /// Draws this slider on the specified graphics object /// </summary> /// <param name="g"></param> public void Draw(Graphics g) { if (_visible == false) { return; } if (_width == 0) { return; } Rectangle bounds = GetBounds(); GraphicsPath gp = new GraphicsPath(); gp.AddRoundedRectangle(bounds, _roundingRadius); LinearGradientBrush lgb = new LinearGradientBrush(bounds, Color.Lighter(.3F), Color.Darker(.3F), LinearGradientMode.ForwardDiagonal); g.FillPath(lgb, gp); Pen l = new Pen(Color.Darker(.2f), 2); Pen r = new Pen(Color.Lighter(.2f), 2); if (_left) { g.DrawLine(l, bounds.Right - 1, bounds.Top, bounds.Right - 1, bounds.Height); } else { g.DrawLine(r, bounds.Left + 1, bounds.Top, bounds.Left + 1, bounds.Right); } l.Dispose(); r.Dispose(); lgb.Dispose(); gp.Dispose(); }
private const double ROUNDED_RECT_RAD_PERCENT = .05d;//5 percent public static GraphicsPath GetGraphicsPath(Rectangle container, ControlShape shape) { GraphicsPath path = new GraphicsPath(); Rectangle pathRect = container; pathRect.Width -= 1; pathRect.Height -= 1; switch (shape) { case ControlShape.Rect: path.AddRectangle(pathRect); break; case ControlShape.RoundedRect: //radius is 10% of smallest side int rad = (int)(Math.Min(pathRect.Height, pathRect.Width) * ROUNDED_RECT_RAD_PERCENT); path.AddRoundedRectangle(pathRect, rad); break; case ControlShape.Circular: path.AddEllipse(pathRect); break; } return(path); }
protected virtual void AddShapePath(GraphicsPath graphicsPath, RegionInfo regionInfo, int sizeOffset = 0) { Rectangle area = regionInfo.Area.SizeOffset(sizeOffset); switch (regionInfo.Shape) { default: case RegionShape.Rectangle: graphicsPath.AddRectangle(area); break; case RegionShape.RoundedRectangle: graphicsPath.AddRoundedRectangle(area, regionInfo.RoundedRectangleRadius); break; case RegionShape.Ellipse: graphicsPath.AddEllipse(area); break; case RegionShape.Triangle: graphicsPath.AddTriangle(area, regionInfo.TriangleAngle); break; case RegionShape.Diamond: graphicsPath.AddDiamond(area); break; } }
/// <summary> /// Controls the actual drawing for this gradient slider control. /// </summary> /// <param name="g"></param> /// <param name="clipRectangle"></param> protected virtual void OnDraw(Graphics g, Rectangle clipRectangle) { GraphicsPath gp = new GraphicsPath(); Rectangle innerRect = new Rectangle(_leftHandle.Width, 3, Width - 1 - _rightHandle.Width - _leftHandle.Width, Height - 1 - 6); gp.AddRoundedRectangle(innerRect, 2); if (Width == 0 || Height == 0) { return; } // Create a rounded gradient effect as the backdrop that other colors will be drawn to LinearGradientBrush silver = new LinearGradientBrush(ClientRectangle, BackColor.Lighter(.2F), BackColor.Darker(.6F), LinearGradientMode.Vertical); g.FillPath(silver, gp); silver.Dispose(); LinearGradientBrush lgb = new LinearGradientBrush(innerRect, MinimumColor, MaximumColor, LinearGradientMode.Horizontal); g.FillPath(lgb, gp); lgb.Dispose(); g.DrawPath(Pens.Gray, gp); gp.Dispose(); if (Enabled) { _leftHandle.Draw(g); _rightHandle.Draw(g); } }
/// <summary> /// Custom drawing code /// </summary> /// <param name="g">The graphics object used for drawing.</param> /// <param name="clipRectangle">The clip rectangle.</param> protected virtual void OnDraw(Graphics g, Rectangle clipRectangle) { Rectangle bounds = new Rectangle(0, 0, Width - 1, Height - 1); // Even when fully transparent, I would like to see a "glass like" reflective appearance Color first = _isDown ? Color.FromArgb(80, Color.DarkCyan).Darker(.3F) : Color.FromArgb(80, Color.LightCyan.Lighter(.3F)); Color second = _isDown ? Color.FromArgb(80, Color.LightCyan.Lighter(.3F)) : Color.FromArgb(80, Color.DarkCyan).Darker(.3F); Color first2 = _isDown ? _color.Darker(.4F) : _color.Lighter(.3F); Color second2 = _isDown ? _color.Lighter(.2F) : _color.Darker(.3F); int rad = Math.Min(Math.Min(RoundingRadius, Width / 2), Height / 2); using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangle(bounds, rad); using (var crystalBrush = new LinearGradientBrush(new Point(0, 0), new Point(bounds.Width, bounds.Height), first, second)) { g.FillPath(crystalBrush, gp); } using (var lgb = new LinearGradientBrush(new Point(0, 0), new Point(bounds.Width, bounds.Height), first2, second2)) { g.FillPath(lgb, gp); } } var bevel2 = BevelRadius * 2; if (Width >= bevel2 && Height >= bevel2) { Rectangle inner = new Rectangle(bounds.Left + BevelRadius, bounds.Top + BevelRadius, bounds.Width - bevel2, bounds.Height - bevel2); using (var gp = new GraphicsPath()) { int rRad = RoundingRadius - BevelRadius; if (rRad < 0) { rRad = 0; } gp.AddRoundedRectangle(inner, rRad); Color cPlain = Color.FromArgb(20, Color.Cyan); using (SolidBrush back = new SolidBrush(BackColor)) { g.FillPath(back, gp); } using (SolidBrush crystalFlat = new SolidBrush(cPlain)) { g.FillPath(crystalFlat, gp); } using (Brush b = new SolidBrush(_color)) { g.FillPath(b, gp); } } } }
private void DrawSelectionHighlight(Graphics g, Rectangle clip, Rectangle gb) { if (_selectedRange == null) { return; } int index = _colorRanges.IndexOf(_selectedRange); if (index < 0) { return; } float left = gb.Left; if (_selectedRange.Range.Maximum < _minimum) { return; } if (_selectedRange.Range.Minimum > _maximum) { return; } if (_selectedRange.Range.Minimum != null) { float rangeLeft = GetPosition(_selectedRange.Range.Minimum.Value); if (rangeLeft > left) { left = rangeLeft; } } float right = gb.Right; if (_selectedRange.Range.Maximum != null) { float rangeRight = GetPosition(_selectedRange.Range.Maximum.Value); if (rangeRight < right) { right = rangeRight; } } Rectangle selectionRect = new Rectangle((int)left, gb.Top, (int)(right - left), gb.Height); if (!clip.IntersectsWith(selectionRect)) { return; } GraphicsPath gp = new GraphicsPath(); gp.AddRoundedRectangle(selectionRect, 2); if (selectionRect.Width != 0 && selectionRect.Height != 0) { LinearGradientBrush lgb = new LinearGradientBrush(selectionRect, Color.FromArgb(241, 248, 253), Color.FromArgb(213, 239, 252), LinearGradientMode.ForwardDiagonal); g.FillPath(lgb, gp); lgb.Dispose(); } gp.Dispose(); }
/// <summary> /// Generates and returns a GraphicsPath that represents the outline of the slider's area /// </summary> /// <returns>A GraphicsPath that represents the outline of the slider's area</returns> protected GraphicsPath GenerateSliderGraphicsPath() { GraphicsPath path = new GraphicsPath(); Rectangle rect = GetSliderRectangleBounds(); path.AddRoundedRectangle(rect, rect.Height); return(path); }
public static void AddRoundedRectangleProper(this GraphicsPath graphicsPath, RectangleF rect, float radius, float penWidth = 1) { if (penWidth == 1) { rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1); } if (rect.Width > 0 && rect.Height > 0) { graphicsPath.AddRoundedRectangle(rect, radius); } }
/// <summary> /// Draws this slider on the specified graphics object /// </summary> /// <param name="g"></param> public void Draw(Graphics g) { if (_visible == false) return; if (_width == 0) return; Rectangle bounds = GetBounds(); GraphicsPath gp = new GraphicsPath(); gp.AddRoundedRectangle(bounds, _roundingRadius); LinearGradientBrush lgb = new LinearGradientBrush(bounds, Color.Lighter(.3F), Color.Darker(.3F), LinearGradientMode.ForwardDiagonal); g.FillPath(lgb, gp); lgb.Dispose(); gp.Dispose(); }
/// <summary> /// Instructs this button to draw itself. /// </summary> /// <param name="g">The graphics surface to draw to.</param> public void Draw(Graphics g) { if (Bounds.Width == 0 || Bounds.Height == 0) { return; } Pen border = null; Brush fill = null; if (!Selected && !Highlighted) { border = new Pen(Color.Gray); fill = new LinearGradientBrush(Bounds, BackColor.Lighter(.2f), BackColor.Darker(.2f), 45); } if (!Selected && Highlighted) { border = new Pen(Color.FromArgb(216, 240, 250)); fill = new LinearGradientBrush(Bounds, Color.FromArgb(245, 250, 253), Color.FromArgb(232, 245, 253), LinearGradientMode.Vertical); } if (Selected && !Highlighted) { border = new Pen(Color.FromArgb(153, 222, 253)); fill = new LinearGradientBrush(Bounds, Color.FromArgb(241, 248, 253), Color.FromArgb(213, 239, 252), LinearGradientMode.Vertical); } if (Selected && Highlighted) { border = new Pen(Color.FromArgb(182, 230, 251)); fill = new LinearGradientBrush(Bounds, Color.FromArgb(232, 246, 253), Color.FromArgb(196, 232, 250), LinearGradientMode.Vertical); } using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangle(Bounds, 2); if (fill != null) { g.FillPath(fill, gp); } if (border != null) { g.DrawPath(border, gp); } } fill?.Dispose(); }
public static void DrawRoundedRectangle(this Graphics g, Brush brush, Pen pen, Rectangle rect, float radius) { using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangle(rect, radius); if (brush != null) { g.FillPath(brush, gp); } if (pen != null) { g.DrawPath(pen, gp); } } }
/// <summary> /// Controls the actual drawing for this gradient slider control. /// </summary> /// <param name="g">The graphics object used for drawing.</param> /// <param name="clipRectangle">The clip rectangle.</param> protected virtual void OnDraw(Graphics g, Rectangle clipRectangle) { using (GraphicsPath gp = new GraphicsPath()) { Rectangle innerRect = new Rectangle(LeftHandle.Width, 3, Width - 1 - RightHandle.Width - LeftHandle.Width, Height - 1 - 6); gp.AddRoundedRectangle(innerRect, 2); if (Width == 0 || Height == 0) { return; } // Create a rounded gradient effect as the backdrop that other colors will be drawn to LinearGradientBrush silver = new LinearGradientBrush(ClientRectangle, BackColor.Lighter(.2F), BackColor.Darker(.6F), LinearGradientMode.Vertical); g.FillPath(silver, gp); silver.Dispose(); using (LinearGradientBrush lgb = new LinearGradientBrush(innerRect, Color.White, Color.White, LinearGradientMode.Horizontal)) { Color[] colors = new Color[37]; float[] positions = new float[37]; for (int i = 0; i <= 36; i++) { int j = _inverted ? 36 - i : i; colors[j] = SymbologyGlobal.ColorFromHsl(((i * 10) + _hueShift) % 360, 1, .7).ToTransparent(.7f); positions[i] = i / 36f; } ColorBlend cb = new ColorBlend { Colors = colors, Positions = positions }; lgb.InterpolationColors = cb; g.FillPath(lgb, gp); } g.DrawPath(Pens.Gray, gp); } if (Enabled) { LeftHandle.Draw(g); RightHandle.Draw(g); } }
/// <summary> /// Controls the drawing of this bar. /// </summary> /// <param name="e">The PaintEventArgs for this paint action</param> protected override void OnPaint(PaintEventArgs e) { Bitmap bmp = new Bitmap(Width, Height); using (Graphics g = Graphics.FromImage(bmp)) { g.SetClip(e.ClipRectangle); using (Brush b = new SolidBrush(BackColor)) { g.FillRectangle(b, ClientRectangle); } RectangleF r = new RectangleF(0f, 0f, Width - 1, Height - 1); using (GraphicsPath gp = new GraphicsPath()) { using (LinearGradientBrush lgb = new LinearGradientBrush(r, Color.LightGreen, Color.Green, LinearGradientMode.Vertical)) { gp.AddRoundedRectangle(new Rectangle(0, 0, Width - 1, Height - 1), 4); int w = Value * (Width - 1) / 100; RectangleF backup = e.Graphics.ClipBounds; g.SetClip(new Rectangle(0, 0, w, Height), CombineMode.Intersect); g.FillPath(lgb, gp); g.SetClip(backup, CombineMode.Replace); } g.DrawPath(Pens.Gray, gp); } if (!ShowMessage) { return; } StringFormat fmt = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; using (Brush fontBrush = new SolidBrush(FontColor)) { g.DrawString(_message, Font, fontBrush, r, fmt); } } e.Graphics.DrawImageUnscaled(bmp, 0, 0); }
/// <summary> /// Draws this slider on the specified graphics object. /// </summary> /// <param name="g">The graphics object used for drawing.</param> public void Draw(Graphics g) { if (!Visible || Width == 0) { return; } Rectangle bounds = GetBounds(); using (GraphicsPath gp = new GraphicsPath()) { gp.AddRoundedRectangle(bounds, RoundingRadius); using (LinearGradientBrush lgb = new LinearGradientBrush(bounds, Color.Lighter(.3F), Color.Darker(.3F), LinearGradientMode.ForwardDiagonal)) { g.FillPath(lgb, gp); } } }
public static GraphicsPath Get3DShinePath(Rectangle container, ControlShape shape) { GraphicsPath path = new GraphicsPath(); Rectangle pathRect = container; pathRect.Width -= 1; pathRect.Height -= 1; RectangleF halfRect = new RectangleF(pathRect.X, pathRect.Y, pathRect.Width, pathRect.Height / 2f); if (pathRect.Height > 0 && pathRect.Width > 0) { switch (shape) { case ControlShape.Rect: path.AddRectangle(halfRect); break; case ControlShape.RoundedRect: //radius is 10% of smallest side int rad = (int)(Math.Min(halfRect.Height, halfRect.Width) * ROUNDED_RECT_RAD_PERCENT); path.AddRoundedRectangle(halfRect, rad); break; case ControlShape.Circular: path.AddArc(pathRect, 180, 142); PointF[] pts = new PointF[] { path.GetLastPoint(), new PointF(container.Width * .70f, container.Height * .33f), new PointF(container.Width * .25f, container.Height * .5f), path.PathPoints[0] }; path.AddCurve(pts); path.CloseFigure(); break; } } return(path); }
private void DrawTips(Graphics g) { int offset = 10; int padding = 3; string tipText; if (isDrawingMode) { tipText = "Ctrl: Region mode ░ Shift: Pen color ░ Mouse wheel: Pen size ░ Space: Fullscreen capture"; } else { tipText = "Ctrl: Drawing mode ░ Space: Fullscreen capture"; } Size textSize = g.MeasureString(tipText, tipFont).ToSize(); int rectWidth = textSize.Width + padding * 2; int rectHeight = textSize.Height + padding * 2; Rectangle textRectangle = new Rectangle(ScreenRectangle0Based.Width / 2 - rectWidth / 2, offset, rectWidth, rectHeight); if (textRectangle.RectangleOffset(10).Contains(CurrentMousePosition0Based)) { textRectangle.Y = ScreenRectangle0Based.Height - rectHeight - offset; } using (GraphicsPath backgroundPath = new GraphicsPath()) using (Brush brush = new SolidBrush(Color.FromArgb(175, Color.White))) using (Pen pen = new Pen(Color.FromArgb(175, Color.Black))) { backgroundPath.AddRoundedRectangle(textRectangle, 5); g.FillPath(brush, backgroundPath); g.DrawPath(pen, backgroundPath); } using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) { g.DrawString(tipText, tipFont, Brushes.Black, textRectangle, sf); } }
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.AddRoundedRectangle(new RectangleF(0, 0, img.Width, img.Height), cornerRadius); using (TextureBrush brush = new TextureBrush(img)) { g.FillPath(brush, gp); } } } return(bmp); }
/// <summary> /// The graphics device and clip rectangle are in the parent coordinates. /// </summary> /// <param name="g">Graphics object used for drawing.</param> /// <param name="clipRectangle">The clip rectangle.</param> /// <param name="symbol">The symbol to use for drawing.</param> public void Draw(Graphics g, Rectangle clipRectangle, ISymbol symbol) { Color topLeft; Color bottomRight; if (IsSelected) { topLeft = _selectionColor.Darker(.3F); bottomRight = _selectionColor.Lighter(.3F); } else { topLeft = _backColor.Lighter(.3F); bottomRight = _backColor.Darker(.3F); } LinearGradientBrush b = new LinearGradientBrush(Bounds, topLeft, bottomRight, LinearGradientMode.ForwardDiagonal); GraphicsPath gp = new GraphicsPath(); gp.AddRoundedRectangle(Bounds, RoundingRadius); g.FillPath(b, gp); gp.Dispose(); b.Dispose(); Matrix old = g.Transform; Matrix shift = g.Transform; shift.Translate(Bounds.Left + (Bounds.Width / 2), Bounds.Top + (Bounds.Height / 2)); g.Transform = shift; if (symbol != null) { OnDrawSymbol(g, symbol); } g.Transform = old; }
public override void AddShapePath(GraphicsPath gp, Rectangle rect) { gp.AddRoundedRectangle(rect, Radius); }
/// <summary> /// Custom drawing code /// </summary> /// <param name="g"></param> /// <param name="clipRectangle"></param> protected virtual void OnDraw(Graphics g, Rectangle clipRectangle) { Brush b = new SolidBrush(_color); Color pressedLight = _color.Lighter(.2F); Color pressedDark = _color.Darker(.4F); Color light = _color.Lighter(.3F); Color dark = _color.Darker(.3F); Rectangle bounds = new Rectangle(0, 0, Width - 1, Height - 1); GraphicsPath gp = new GraphicsPath(); // Even when fully transparent, I would like to see a "glass like" reflective appearance LinearGradientBrush crystalBrush; Color cLight = Color.FromArgb(80, Color.LightCyan.Lighter(.3F)); Color cDark = Color.FromArgb(80, Color.DarkCyan).Darker(.3F); if (_isDown == false) { crystalBrush = new LinearGradientBrush(new Point(0, 0), new Point(bounds.Width, bounds.Height), cLight, cDark); } else { crystalBrush = new LinearGradientBrush(new Point(0, 0), new Point(bounds.Width, bounds.Height), cDark, cLight); } LinearGradientBrush lgb; if (_isDown == false) { lgb = new LinearGradientBrush(new Point(0, 0), new Point(bounds.Width, bounds.Height), light, dark); } else { lgb = new LinearGradientBrush(new Point(0, 0), new Point(bounds.Width, bounds.Height), pressedDark, pressedLight); } int rad = Math.Min(Math.Min(_roundingRadius, Width / 2), Height / 2); gp.AddRoundedRectangle(bounds, rad); g.FillPath(crystalBrush, gp); g.FillPath(lgb, gp); gp.Dispose(); if (Width < _bevelRadius * 2 || Height < _bevelRadius * 2) { } else { Rectangle inner = new Rectangle(bounds.Left + _bevelRadius, bounds.Top + _bevelRadius, bounds.Width - _bevelRadius * 2, bounds.Height - _bevelRadius * 2); gp = new GraphicsPath(); int rRad = _roundingRadius - _bevelRadius; if (rRad < 0) { rRad = 0; } gp.AddRoundedRectangle(inner, rRad); Color cPlain = Color.FromArgb(20, Color.Cyan); SolidBrush back = new SolidBrush(BackColor); SolidBrush crystalFlat = new SolidBrush(cPlain); g.FillPath(back, gp); g.FillPath(crystalFlat, gp); back.Dispose(); crystalFlat.Dispose(); g.FillPath(b, gp); gp.Dispose(); } b.Dispose(); }
protected override void AddShapePath(GraphicsPath graphicsPath, Rectangle rect) { graphicsPath.AddRoundedRectangle(rect, Radius); }
/// <summary> /// Draws the slider itself in client coordinates /// </summary> /// <param name="g"></param> /// <param name="clipRectangle"></param> protected virtual void OnDrawSlider(Graphics g, Rectangle clipRectangle) { Color light = _sliderColor.Lighter(.3F); Color dark = _sliderColor.Darker(.3F); Point tl = new Point(); Point br = new Point(); double val = (Value - Minimum) / (Maximum - Minimum); int x, y, w, h; if (_orientation == Orientation.Horizontal) { w = 10; int span = Width - w - 1; float vSpan = _rampRectangle.Height - _rampRadius * 2; y = _rampRectangle.Top; if (_flipRamp) { x = (int)(span * (1 - val)); // h = (int)(_rampRadius * 2 + vSpan * (1 - val)); } else { x = (int)(val * span); } h = (int)(_rampRadius * 2 + vSpan * val); if (h < 10) { h = 10; } if (_invertRamp == false) { y = _rampRectangle.Bottom - h; } } else { h = 10; int span = Height - h - 1; x = _rampRectangle.Left; float hSpan = _rampRectangle.Width - _rampRadius * 2; if (_flipRamp) { y = (int)(span * val); //w = (int)(_rampRadius * 2 + hSpan * (1 - val)); } else { y = (int)(span * (1 - val)); } w = (int)(_rampRadius * 2 + hSpan * val); if (w < 10) { w = 10; } if (_invertRamp == false) { x = _rampRectangle.Right - w; } } if (x > _rampRectangle.Width) { x = _rampRectangle.Width; } if (x < 0) { x = 0; } if (y > _rampRectangle.Height) { y = _rampRectangle.Height; } if (y < 0) { y = 0; } tl.X = x; tl.Y = y; br.X = x + w; br.Y = y + h; try { LinearGradientBrush lgb = new LinearGradientBrush(tl, br, light, dark); Rectangle bounds = new Rectangle(x, y, w, h); _sliderRectangle = bounds; GraphicsPath gp = new GraphicsPath(); gp.AddRoundedRectangle(bounds, (int)Math.Round(_sliderRadius)); g.FillPath(lgb, gp); g.DrawPath(Pens.Gray, gp); gp.Dispose(); GraphicsPath bottomRight = new GraphicsPath(); bottomRight.AddRoundedRectangleBottomRight(bounds, (int)Math.Round(_sliderRadius)); g.DrawPath(Pens.DarkGray, bottomRight); bottomRight.Dispose(); lgb.Dispose(); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
public override void OnShapePathRequested(GraphicsPath gp, RectangleF rect) { gp.AddRoundedRectangle(rect, CornerRadius); }
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) { Picture = img }; 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 (Bitmap bmpWatermark = new Bitmap(watermarkSize.Width, watermarkSize.Height)) using (Graphics gWatermark = Graphics.FromImage(bmpWatermark)) { gWatermark.SetHighQuality(); if (DrawBackground) { using (GraphicsPath backgroundPath = new GraphicsPath()) { Rectangle backgroundRect = new Rectangle(0, 0, watermarkSize.Width, watermarkSize.Height); backgroundPath.AddRoundedRectangle(backgroundRect, CornerRadius); Brush backgroundBrush = null; try { if (UseGradient) { backgroundBrush = new LinearGradientBrush(backgroundRect, BackgroundColor, BackgroundColor2, GradientType); if (UseCustomGradient && CustomGradientList != null && CustomGradientList.Count > 1) { ColorBlend colorBlend = new ColorBlend(); IEnumerable <GradientStop> gradient = CustomGradientList.OrderBy(x => x.Offset); colorBlend.Colors = gradient.Select(x => x.Color).ToArray(); colorBlend.Positions = gradient.Select(x => x.Offset).ToArray(); ((LinearGradientBrush)backgroundBrush).InterpolationColors = colorBlend; } } else { backgroundBrush = new SolidBrush(BackgroundColor); } gWatermark.FillPath(backgroundBrush, backgroundPath); } finally { if (backgroundBrush != null) { backgroundBrush.Dispose(); } } using (Pen borderPen = new Pen(BorderColor)) { gWatermark.DrawPath(borderPen, backgroundPath); } } } gWatermark.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) { float centerX = bmpWatermark.Width / 2f; float centerY = bmpWatermark.Height / 2f; if (DrawTextShadow) { using (Brush textShadowBrush = new SolidBrush(TextShadowColor)) { gWatermark.DrawString(parsedText, textFont, textShadowBrush, centerX + TextShadowOffset.X, centerY + TextShadowOffset.Y, sf); } } using (Brush textBrush = new SolidBrush(TextColor)) { gWatermark.DrawString(parsedText, textFont, textBrush, centerX, centerY, sf); } } using (Graphics gResult = Graphics.FromImage(img)) { gResult.SetHighQuality(); gResult.DrawImage(bmpWatermark, watermarkRectangle); } } } return(img); }
private Image DrawWatermarkText(Image img, string drawText) { if (!string.IsNullOrEmpty(drawText) && Config.WatermarkFont.Size > 0) { Font font = null; Brush backgroundBrush = null; try { int offset = Config.WatermarkOffset; font = Config.WatermarkFont; Size textSize = TextRenderer.MeasureText(drawText, font); Size labelSize = new Size(textSize.Width + 10, textSize.Height + 10); Point labelPosition = FindPosition(Config.WatermarkPositionMode, offset, img.Size, new Size(textSize.Width + 10, textSize.Height + 10), 1); if (Config.WatermarkAutoHide && ((img.Width < labelSize.Width + offset) || (img.Height < labelSize.Height + offset))) { return(img); } Rectangle labelRectangle = new Rectangle(Point.Empty, labelSize); Color fontColor = Config.WatermarkFontArgb; using (Bitmap bmp = new Bitmap(labelRectangle.Width + 1, labelRectangle.Height + 1)) using (Graphics g = Graphics.FromImage(bmp)) { g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; if (Config.WatermarkUseCustomGradient) { backgroundBrush = GradientMaker.CreateGradientBrush(labelRectangle.Size, Config.WatermarkGradient); } else { backgroundBrush = new LinearGradientBrush(labelRectangle, Config.WatermarkGradient1Argb, Config.WatermarkGradient2Argb, Config.WatermarkGradientType); } using (GraphicsPath gPath = new GraphicsPath()) using (Pen borderPen = new Pen(Config.WatermarkBorderArgb)) { gPath.AddRoundedRectangle(labelRectangle, Config.WatermarkCornerRadius); g.FillPath(backgroundBrush, gPath); g.DrawPath(borderPen, gPath); } using (Brush textBrush = new SolidBrush(fontColor)) using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) { g.DrawString(drawText, font, textBrush, bmp.Width / 2f, bmp.Height / 2f, sf); } using (Graphics gImg = Graphics.FromImage(img)) { gImg.SmoothingMode = SmoothingMode.HighQuality; gImg.DrawImage(bmp, labelPosition.X, labelPosition.Y, bmp.Width, bmp.Height); if (Config.WatermarkAddReflection) { using (Bitmap bmp2 = ImageHelpers.AddReflection(bmp, 50, 150, 10)) { gImg.DrawImage(bmp2, new Rectangle(labelPosition.X, labelPosition.Y + bmp.Height - 1, bmp2.Width, bmp2.Height)); } } } } } catch (Exception ex) { DebugHelper.WriteException(ex, "Error while drawing watermark"); } finally { if (font != null) { font.Dispose(); } if (backgroundBrush != null) { backgroundBrush.Dispose(); } } } return(img); }
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) { Picture = img }; 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.AddRoundedRectangle(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); }