이미지 중심을 기준으로 해당 크기로 자른다.

 

        public static Bitmap CropCenterBitmap(Bitmap src, int w, int h)
        {
            if (src == null)
                return null;

            int width = src.Width;
            int height = src.Height;

            if (width < w && height < h)
                return src;

            int x = 0;
            int y = 0;

            if (width > w)
                x = (width - w)/ 2;

            if (height > h)
                y = (height - h)/ 2;

            int cw = w; // crop width
            int ch = h; // crop height

            if (w > width)
                cw = width;

            if (h > height)
                ch = height;

            return Bitmap.CreateBitmap(src, x, y, cw, ch);
        }

+ Recent posts