/// <summary> /// Initialize a new instance of the class <see cref="Smoothing"/> by setting the smoothing quality of the lines. /// </summary> /// <param name="graphics">Surface <see cref="Graphics"/> oriented on which to draw</param> /// <param name="quality">One of the values in the enumeration <see cref="SmoothingModeEx"/> that represents the smoothing quality of the lines.</param> public Smoothing(Graphics graphics, SmoothingModeEx quality) { Graphics safeGraphics = SentinelHelper.PassThroughNonNull(graphics); _graphics = safeGraphics; _smoothingModePrev = _graphics.SmoothingMode; _graphics.SmoothingMode = quality.ToSmoothingMode(); }
/// <summary> /// Equivalence between the types <see cref="SmoothingModeEx" /> and <see cref="SmoothingMode"/>. /// </summary> /// <param name="mode">One of the values in the enumeration <see cref="SmoothingModeEx"/> that represents the smoothing quality of the lines.</param> /// <returns> /// Equivalent representation quality. /// </returns> public static SmoothingMode ToSmoothingMode(this SmoothingModeEx mode) { SmoothingMode smmode = SmoothingMode.AntiAlias; if (mode == SmoothingModeEx.HighSpeed) { smmode = SmoothingMode.Default; } return(smmode); }
/// <summary> /// Initialize a new instance of the class <see cref="Smoothing"/> by setting the smoothing quality of the lines. /// </summary> /// <param name="canvas">Surface <see cref="Graphics"/> oriented on which to draw.</param> /// <param name="quality">One of the values in the enumeration <see cref="SmoothingModeEx"/> that represents the smoothing quality of the lines.</param> public Smoothing(Canvas canvas, SmoothingModeEx quality) : this(SentinelHelper.PassThroughNonNull(canvas).Graphics, quality) { }
/// <summary> /// Returns a <see cref="Brush"/> to draw from a reference image specifying the orientation, quality, style and effect. /// </summary> /// <param name="image">An <see cref="Image"/> base object to create the drawing brush.</param> /// <param name="rect">A <see cref="RectangleF"/> structure that represents the rectangle in which to paint.</param> /// <param name="style">One of the values in the enumeration <see cref="ImageStyle"/> that represents image style.</param> /// <param name="effect">One of the values in the enumeration <see cref="EffectType"/> that represents the type of effect to be applied.</param> /// <param name="orientation">One of the values in the enumeration <see cref="Orientation"/> that represents the orientation of the brush.</param> /// <param name="quality">One of the values in the enumeration <see cref="SmoothingModeEx"/> that represents the quality of presentation</param> /// <returns> /// Returns a <see cref="Brush"/> object reference that represents the drawing brush. /// </returns> public static Brush ToBrush(this Image image, RectangleF rect, ImageStyle style, EffectType effect, Orientation orientation, SmoothingModeEx quality) { if (image == null) { return(new SolidBrush(Color.Empty)); } Brush brush; TextureBrush tempTextureBrush = null; Image texture = image.Rotate(orientation).ApplyEffect(effect); using (var bitmap = new Bitmap(texture.Width, texture.Height)) using (var graphics = Graphics.FromImage(bitmap)) using (new Smoothing(graphics, quality)) { Rectangle dstRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle srcRect = new Rectangle(Point.Empty, texture.Size); graphics.DrawImage(texture, dstRect, srcRect, GraphicsUnit.Pixel); try { tempTextureBrush = new TextureBrush(bitmap) { WrapMode = style.ToWrapMode() }; switch (style) { case ImageStyle.TopLeft: tempTextureBrush.TranslateTransform(rect.Left, rect.Top); break; case ImageStyle.TopMiddle: tempTextureBrush.TranslateTransform(rect.Left + (rect.Width - texture.Width) / 2, rect.Top); break; case ImageStyle.TopRight: tempTextureBrush.TranslateTransform(rect.Right - texture.Width, rect.Top); break; case ImageStyle.CenterLeft: tempTextureBrush.TranslateTransform(rect.Left, rect.Top + (rect.Height - texture.Height) / 2); break; case ImageStyle.CenterMiddle: tempTextureBrush.TranslateTransform(rect.Left + (rect.Width - texture.Width) / 2, rect.Top + (rect.Height - texture.Height) / 2); break; case ImageStyle.CenterRight: tempTextureBrush.TranslateTransform(rect.Right - texture.Width, rect.Top + (rect.Height - texture.Height) / 2); break; case ImageStyle.BottomLeft: tempTextureBrush.TranslateTransform(rect.Left, rect.Bottom - texture.Height); break; case ImageStyle.BottomMiddle: tempTextureBrush.TranslateTransform(rect.Left + (rect.Width - texture.Width) / 2, rect.Bottom - texture.Height); break; case ImageStyle.BottomRight: tempTextureBrush.TranslateTransform(rect.Right - texture.Width, rect.Bottom - texture.Height); break; case ImageStyle.Stretch: tempTextureBrush.TranslateTransform(rect.Left, rect.Top); tempTextureBrush.ScaleTransform(rect.Width / texture.Width, rect.Height / texture.Height); break; case ImageStyle.Tile: case ImageStyle.TileFlipX: case ImageStyle.TileFlipY: case ImageStyle.TileFlipXY: tempTextureBrush.TranslateTransform(rect.Left, rect.Top); break; } brush = (Brush)tempTextureBrush.Clone(); } finally { tempTextureBrush?.Dispose(); } } return(brush); }