Duality.ExtMethodsBitmap.Resize C# (CSharp) Method

Resize() public static method

Creates a resized version of a Bitmap. Gained space will be empty, lost space will crop the image.
public static Resize ( this bm, int w, int h, Alignment origin = Alignment.TopLeft ) : Bitmap
bm this The original Bitmap.
w int The desired width.
h int The desired height.
origin Alignment The desired resize origin in the original image.
return System.Drawing.Bitmap
        public static Bitmap Resize(this Bitmap bm, int w, int h, Alignment origin = Alignment.TopLeft)
        {
            int x = 0;
            int y = 0;

            if (origin == Alignment.Right ||
                origin == Alignment.TopRight ||
                origin == Alignment.BottomRight)
                x = w - bm.Width;
            else if (
                origin == Alignment.Center ||
                origin == Alignment.Top ||
                origin == Alignment.Bottom)
                x = (w - bm.Width) / 2;

            if (origin == Alignment.Bottom ||
                origin == Alignment.BottomLeft ||
                origin == Alignment.BottomRight)
                y = h - bm.Height;
            else if (
                origin == Alignment.Center ||
                origin == Alignment.Left ||
                origin == Alignment.Right)
                y = (h - bm.Height) / 2;

            return bm.SubImage(-x, -y, w, h);
        }