コード例 #1
0
        /// <summary>
        ///     Crops the image to a centered square.
        ///     Make sure that if you're pulling the source image from a stream,
        ///     the stream remains open.
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static byte[] CropImageToSquare(Image img)
        {
            CropDimensionHelper d        = GetCropDimensions(img.Width, img.Height);
            Rectangle           cropArea = CreateRectangleFromDimensions(d);
            Bitmap bmp = CreateBitmapFromDimensions(d.Smaller, d.Smaller);

            DrawSquareOntoBitmap(bmp, img, cropArea);
            byte[] squareBytes = GetImageBytes(bmp, img.RawFormat);
            return(squareBytes);
        }
コード例 #2
0
        /// <summary>
        ///     Gets dimensions for our square.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private static CropDimensionHelper GetCropDimensions(int width, int height)
        {
            CropDimensionHelper d = new CropDimensionHelper();

            if (width < height)
            {
                d.WidthStart  = 0;
                d.HeightStart = (height / 2) - (width / 2);
                d.Smaller     = width;
            }
            else
            {
                d.HeightStart = 0;
                d.WidthStart  = (width / 2) - (height / 2);
                d.Smaller     = height;
            }

            return(d);
        }
コード例 #3
0
        /// <summary>
        ///     Creates a crop area from our dimensions.
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        private static Rectangle CreateRectangleFromDimensions(CropDimensionHelper d)
        {
            Rectangle cropArea = new Rectangle(d.WidthStart, d.HeightStart, d.Smaller, d.Smaller);

            return(cropArea);
        }