public ImageProcessTarget(Size targetSize, ImageCropType cropType, float cropWidthPercentage, float cropHeightPercentage)
        {
            this.ScaleSize = targetSize;
            this.CropType = cropType;

            this.CropSize = new Size(
                    (int)Math.Round(this.ScaleSize.Width * cropWidthPercentage, 0),
                    (int)Math.Round(this.ScaleSize.Height * cropHeightPercentage, 0)
                );
        }
Example #2
0
        /// <summary>
        /// Crops an image using the specified crop type, width and height
        /// <para>This method makes use of Crop(Bitmap image, Rectangle rectangle), see that method for more information</para>
        /// </summary>
        /// <param name="image">The image to crop</param>
        /// <param name="imageCropType">The type of crop</param>
        /// <param name="width">The desired width</param>
        /// <param name="height">The desired height</param>
        /// <returns>A new image of with the specified crop</returns>
        public Bitmap Crop(Bitmap image, ImageCropType imageCropType, int width, int height)
        {
            var x = 0;
            var y = 0;

            switch (imageCropType)
            {
                case ImageCropType.Center:
                    x = (image.Width - width) / 2;
                    y = (image.Height - height) / 2;
                    break;
                case ImageCropType.TopRight:
                    x = image.Width - width;
                    break;
                case ImageCropType.BottomLeft:
                    y = image.Height - height;
                    break;
                case ImageCropType.BottomRight:
                    x = image.Width - width;
                    y = image.Height - height;
                    break;
                case ImageCropType.LeftCenter:
                    y = (image.Height - height) / 2;
                    break;
                case ImageCropType.TopCenter:
                    x = (image.Width - width) / 2;
                    break;
                case ImageCropType.RightCenter:
                    x = image.Width - width;
                    y = (image.Height - height) / 2;
                    break;
                case ImageCropType.BottomCenter:
                    x = (image.Width - width) / 2;
                    y = image.Height - height;
                    break;
                default:
                    // this will be TopLeft (0, 0)
                    break;
            }

            return this.Crop(image, new Rectangle(x, y, width, height));
        }
Example #3
0
 public void SetAppLogoImageCrop(ImageCropType appLogoImageCrop)
 {
     this.appLogoImageCrop = appLogoImageCrop;
 }
Example #4
0
        private void CropCornerAndTestForBlack(ImageCropType type, Rectangle blackRect, float blackPortion = 0.25f)
        {
            var source = ResHelper.LoadImage("croppable.jpg");

            var result = this.subject.Crop(source, type, 100, 100);
            Assert.IsNotNull(result);

            // allowing 2 * width as epsilon to handle integer division / rounding
            Assert.AreEqual(result.Width * result.Height * (1 - blackPortion), ResHelper.PixelCount(result, Color.White), result.Width * 2);
            Assert.AreEqual(result.Width * result.Height * blackPortion, ResHelper.PixelCount(result, Color.Black, blackRect), result.Width * 2);
        }