Exemple #1
0
        /// <summary>
        /// Scales an image to fill the specified bounds while maintaining aspect ratio.
        /// Any part of the image that spills outside the bounds will be cropped out.
        /// </summary>
        /// <param name="imageData">An array containing the image data.</param>
        /// <param name="width">The width, as the number of pixels, to for the bounds.</param>
        /// <param name="height">The height, as the number of pixels, to for the bounds.</param>
        /// <returns>A scaled image.</returns>
        public byte[] ScaleToFill(byte[] imageData, int width, int height)
        {
            Throw.IfArgumentNull(imageData, "imageData");

            if (width < 1)
            {
                throw new ArgumentException("The width (" + width + ") must be greater than zero.", nameof(width));
            }

            if (height < 1)
            {
                throw new ArgumentException("The height (" + height + ") must be greater than zero.", nameof(height));
            }

            UIImage image = UIImageByteConverter.FromBytes(imageData);

            if (image.Size.Width != width || image.Size.Height != height)
            {
                RectangleF newBounds =
                    this.CalculateBoundsForScaling(
                        image.CGImage.Width,
                        image.CGImage.Height,
                        image.Orientation,
                        Math.Max(width, height),
                        ScaleMode.Fill);

                return(this.Scale(image, new CGSize(width, height), newBounds));
            }

            return(imageData);
        }
Exemple #2
0
        /// <summary>
        /// Scales an image to fit within the maximum defined pixel resolution.
        /// </summary>
        /// <param name="imageData">An array containing the image data.</param>
        /// <param name="maxResolution">The maximum resolution, in number of pixels.</param>
        /// <returns>A scalled image.</returns>
        public byte[] ScaleToFit(byte[] imageData, int maxResolution)
        {
            Throw.IfArgumentNull(imageData, "imageData");

            if (maxResolution < 1)
            {
                throw new ArgumentException("The maximum resolution (" + maxResolution + ") must be greater than zero.", nameof(maxResolution));
            }

            UIImage image = UIImageByteConverter.FromBytes(imageData);

            if (image.CGImage.Width > maxResolution || image.CGImage.Height > maxResolution)
            {
                RectangleF newBounds =
                    this.CalculateBoundsForScaling(
                        image.CGImage.Width,
                        image.CGImage.Height,
                        image.Orientation,
                        maxResolution,
                        ScaleMode.Fit);

                return(this.Scale(image, newBounds.Size, newBounds));
            }

            return(imageData);
        }
Exemple #3
0
        private byte[] Scale(UIImage imageToScale, CGSize destinationSize, CGRect destinationRect)
        {
            UIGraphics.BeginImageContext(destinationSize);

            imageToScale.Draw(destinationRect);

            UIImage scaledImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(UIImageByteConverter.ToBytesAsJpeg(scaledImage, this.compressionQuality));
        }