Duality.ExtMethodsBitmap.Crop C# (CSharp) Method

Crop() public static method

Creates a cropped version of the specified Bitmap, removing transparent / empty border areas.
public static Crop ( this bm, bool cropX = true, bool cropY = true ) : Bitmap
bm this The original Bitmap.
cropX bool Whether the image should be cropped in X-direction
cropY bool Whether the image should be cropped in Y-direction
return System.Drawing.Bitmap
        public static Bitmap Crop(this Bitmap bm, bool cropX = true, bool cropY = true)
        {
            if (!cropX && !cropY) return bm.Clone() as Bitmap;
            Rectangle bounds = bm.OpaqueBounds();
            return bm.SubImage(cropX ? bounds.X : 0, cropY ? bounds.Y : 0, cropX ? bounds.Width : bm.Width, cropY ? bounds.Height : bm.Height);
        }