ActivEarth.Account.EditProfile.createSquarePhotoSize C# (CSharp) Method

createSquarePhotoSize() private method

Creates a square cut of the image at originalPath and places it in /Images/Account/UserProfile/sizeName/*subdirectory*/userID.png
private createSquarePhotoSize ( string originalPath, string sizeName, int userID, int squareImgLen ) : void
originalPath string The path to the original image to resize.
sizeName string A name for the size, it will be put in that directory. Will be created if it does not exist.
userID int The ID of the user to generate the image for. The photo will be put in a subdirecory of sizename based on userID.
squareImgLen int The side length of the square.
return void
        private void createSquarePhotoSize(string originalPath, string sizeName, int userID, int squareImgLen)
        {
            string newFilePath = getSizedIconPath(userID, sizeName, ".png");

            Stream inFile = null;
            Stream outFile = null;

            try
            {
                inFile = new FileStream(originalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                outFile = new FileStream(newFilePath, FileMode.Create, FileAccess.Write, FileShare.Write);

                using (var img = System.Drawing.Image.FromStream(inFile))
                using (var icon = new System.Drawing.Bitmap(squareImgLen, squareImgLen))
                {
                    var smallerSide = Math.Min(img.Width, img.Height);
                    var centerX = (img.Width - smallerSide) / 2;
                    var centerY = (img.Height - smallerSide) / 2;

                    using (var iconGraphic = System.Drawing.Graphics.FromImage(icon))
                    {
                        iconGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        iconGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        iconGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                        var cutRect = new System.Drawing.Rectangle(0, 0, squareImgLen, squareImgLen);
                        iconGraphic.DrawImage(img, cutRect, centerX, centerY, smallerSide, smallerSide, System.Drawing.GraphicsUnit.Pixel);
                        icon.Save(outFile, System.Drawing.Imaging.ImageFormat.Png);
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (inFile != null)
                {
                    inFile.Close();
                }
                if (outFile != null)
                {
                    outFile.Close();
                }
            }
        }