Exemplo n.º 1
0
        public byte[] ResizeImage(byte[] imageData, float width, float height)
        {
            UIImage originalImage    = ImageUtils.FromByteArray(imageData);
            float   maxWidth         = 200;
            float   maxHeight        = 200;
            float   calculatedWidth  = width;
            float   calculatedHeight = height;

            var sourceSize      = originalImage.Size;
            var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);

            if (maxResizeFactor > 1)
            {
                return(originalImage.AsJPEG().ToArray());
            }
            calculatedWidth  = (float)maxResizeFactor * (float)sourceSize.Width;
            calculatedHeight = (float)maxResizeFactor * (float)sourceSize.Height;

            UIGraphics.BeginImageContext(new SizeF(calculatedWidth, calculatedHeight));
            originalImage.Draw(new RectangleF(0, 0, calculatedWidth, calculatedHeight));
            var resultImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            string AvatarImageName    = "avatar.jpg";
            string PersonalFolderPath = "";
            string avatarFilename     = System.IO.Path.Combine(PersonalFolderPath, AvatarImageName);

            resultImage.AsJPEG().Save(avatarFilename, true);

            return(resultImage.AsJPEG().ToArray());
        }