public Image Inflate(int left, int top, int right, int bottom, BorderType borderType, uint borderValue) { // calculate and verify target area in source coordinates Rectangle bounds = Rectangle.FromLTRB( -left, -top, this.Width + right, this.Height + bottom); if (bounds.Width <= 0) { throw new ArgumentException("The new image width is invalid."); } if (bounds.Height <= 0) { throw new ArgumentException("The new image height is invalid."); } Image dst = new Image(bounds.Size, this); // calculate source area to copy from Rectangle srcarea = Rectangle.Intersect(bounds, this.Bounds); // calculate destination area to copy to Rectangle dstarea = Rectangle.Offset(srcarea, -bounds.X, -bounds.Y); Image.CopyArea(dst, dstarea.X, dstarea.Y, srcarea.Width, srcarea.Height, this, srcarea.X, srcarea.Y); // set border dst.SetBorder(dstarea, borderType, borderValue); dst.AppendTransform(new MatrixTransform(left, top)); return(dst); }
/// <summary> /// Crops the <see cref="Image"/> using rectangle specified by a pair of coordinates, a width, and a height. /// </summary> /// <param name="x">The x-coordinate of the upper-left corner of the area.</param> /// <param name="y">The y-coordinate of the upper-left corner of the area.</param> /// <param name="width">The width of the area.</param> /// <param name="height">The height of the area.</param> /// <returns> /// A new cropped <see cref="Image"/>. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <para>The rectangular area described by <paramref name="x"/>, <paramref name="y"/>, <paramref name="width"/> and <paramref name="height"/> is outside of this <see cref="Image"/> bounds.</para> /// </exception> public Image Crop(int x, int y, int width, int height) { this.ValidateArea(x, y, width, height); Image dst = new Image(width, height, this); Image.CopyArea(dst, 0, 0, width, height, this, x, y); dst.AppendTransform(new MatrixTransform(-x, -y)); return(dst); }
/// <summary> /// Copies a rectangular area specified by /// a pair of coordinates, a width, and a height from the specified <see cref="Image"/> /// to the current <see cref="Image"/> in-place. /// </summary> /// <param name="x">The x-coordinate, in pixels, of the upper-left corner of the destination rectangle.</param> /// <param name="y">The y-coordinate, in pixels, of the upper-left corner of the destination rectangle.</param> /// <param name="width">The width, in pixels, of the destination rectangle.</param> /// <param name="height">The height, in pixels, of the destination rectangle.</param> /// <param name="src">The <see cref="Image"/> to copy from.</param> /// <param name="xsrc">The x-coordinate, in pixels, of the upper-left corner of the source rectangle.</param> /// <param name="ysrc">The y-coordinate, in pixels, of the upper-left corner of the source rectangle.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="src"/> is <b>null</b>. /// </exception> /// <exception cref="ArgumentException"> /// The images have a different depth. /// The <see cref="Image{T}.BitsPerPixel"/> properties of <paramref name="src"/> and this <see cref="Image"/> are not the same. /// </exception> public void CopyFrom(int x, int y, int width, int height, Image src, int xsrc, int ysrc) { if (src == null) { throw new ArgumentNullException(nameof(src)); } this.ValidateArea(x, y, width, height); src.ValidateArea(xsrc, ysrc, width, height); if (this.BitsPerPixel != src.BitsPerPixel) { throw new ArgumentException(Properties.Resources.E_DepthNotTheSame); } Image.CopyArea(this, x, y, width, height, src, xsrc, ysrc); }
/// <summary> /// Crops the <see cref="Image"/> using rectangle calculated by <see cref="Image.BlackArea"/> method. /// </summary> /// <param name="dx">The amount by which to expand or shrink the left and right sides of the image black area.</param> /// <param name="dy">The amount by which to expand or shrink the top and bottom sides of the image black area.</param> /// <returns> /// A new cropped <see cref="Image"/>. /// </returns> /// <exception cref="NotSupportedException"> /// <para> /// The <see cref="Image{T}.BitsPerPixel"/> is not 1. /// </para> /// </exception> public Image CropBlackArea(int dx, int dy) { // determine black area of the image Rectangle blackArea = this.BlackArea(); if (dx == 0 && dy == 0) { // no frame - simply crop the black area return(this.Crop(blackArea)); } // expand target area Rectangle bounds = Rectangle.Inflate(blackArea, dx, dy); Image dst = new Image(bounds.Size, this); if (!blackArea.IsEmpty) { Rectangle srcarea = Rectangle.Intersect(bounds, blackArea); Rectangle dstarea = Rectangle.Offset(srcarea, -bounds.X, -bounds.Y); Image.CopyArea(dst, dstarea.X, dstarea.Y, srcarea.Width, srcarea.Height, this, srcarea.X, srcarea.Y); if (this.BitsPerPixel > 1) { // set frame to white dst.SetWhiteBorder(dstarea); } } else { if (this.BitsPerPixel > 1) { // set all image to white Vectors.Set(dst.Bits.Length, ulong.MaxValue, dst.Bits, 0); } } dst.AppendTransform(new MatrixTransform(-bounds.X, -bounds.Y)); return(dst); }
private static void CopyArea(Image dst, Rectangle dstBounds, Image src, Point srcOrigin) => Image.CopyArea(dst, dstBounds.X, dstBounds.Y, dstBounds.Width, dstBounds.Height, src, srcOrigin.X, srcOrigin.Y);