Ejemplo n.º 1
0
        public CropImageResponse CropImage(ImageCropDto crop)
        {
            var response = new CropImageResponse();

            if (image.Width >= crop.TargetWidth && image.Height >= crop.TargetHeight)
            {
                var selectedAspectRatio = this.GetAspectRatio(crop.SelectedWidth, crop.SelectedHeight);
                var targetAspectRatio   = this.GetAspectRatio(crop.TargetWidth, crop.TargetHeight);

                if (image.Width == crop.TargetWidth && image.Height == crop.TargetHeight)
                {
                    // if the image is the same size as the target crop:
                    response.Status       = StatusCode.OK;
                    response.CroppedImage = this.image;
                }
                else if (selectedAspectRatio == targetAspectRatio)
                {
                    // if the selected crop has the same aspect ratio as the target crop:
                    if (image.Width == crop.SelectedWidth && image.Height == crop.SelectedHeight)
                    {
                        // if the selected crop is the same size as the original image, no crop is needed:
                        response.Status       = StatusCode.OK;
                        response.CroppedImage = this.image.Resize(crop.TargetWidth);
                    }
                    else
                    {
                        // if the selected crop is smaller than the original image:
                        var cropArea = new Rectangle(crop.X, crop.Y, crop.SelectedWidth, crop.SelectedHeight);
                        using (var croppedImage = this.image.Crop(cropArea))
                        {
                            if (croppedImage.Width == crop.TargetWidth && croppedImage.Height == crop.TargetHeight)
                            {
                                response.Status       = StatusCode.OK;
                                response.CroppedImage = (Image)croppedImage.Clone();
                            }
                            else
                            {
                                response.Status       = StatusCode.OK;
                                response.CroppedImage = croppedImage.Resize(crop.TargetWidth);
                            }
                        }
                    }
                }
            }
            else
            {
                response.Status = StatusCode.BadRequest;
            }

            return(response);
        }
Ejemplo n.º 2
0
		public CropImageResponse CropImage(ImageCropDto crop)
		{
			var response = new CropImageResponse();

			if (image.Width >= crop.TargetWidth && image.Height >= crop.TargetHeight)
			{
				var selectedAspectRatio = this.GetAspectRatio(crop.SelectedWidth, crop.SelectedHeight);
				var targetAspectRatio = this.GetAspectRatio(crop.TargetWidth, crop.TargetHeight);

				if (image.Width == crop.TargetWidth && image.Height == crop.TargetHeight)
				{
					// if the image is the same size as the target crop:
					response.Status = StatusCode.OK;
					response.CroppedImage = this.image;
				}
				else if (selectedAspectRatio == targetAspectRatio)
				{
					// if the selected crop has the same aspect ratio as the target crop:
					if (image.Width == crop.SelectedWidth && image.Height == crop.SelectedHeight)
					{
						// if the selected crop is the same size as the original image, no crop is needed:
						response.Status = StatusCode.OK;
						response.CroppedImage = this.image.Resize(crop.TargetWidth);
					}
					else
					{
						// if the selected crop is smaller than the original image:
						var cropArea = new Rectangle(crop.X, crop.Y, crop.SelectedWidth, crop.SelectedHeight);
						using (var croppedImage = this.image.Crop(cropArea))
						{
							if (croppedImage.Width == crop.TargetWidth && croppedImage.Height == crop.TargetHeight)
							{
								response.Status = StatusCode.OK;
								response.CroppedImage = (Image)croppedImage.Clone();
							}
							else
							{
								response.Status = StatusCode.OK;
								response.CroppedImage = croppedImage.Resize(crop.TargetWidth);
							}
						}
					}
				}
			}
			else
			{
				response.Status = StatusCode.BadRequest;
			}

			return response;
		}