Skip to main content

Resize image and save without saving it to temporary file, C#

Upload an image after resizing to specific size without saving it to temporary file.      


ResizeImageAndSave(fileImage.PostedFile.InputStream, Width, Height, FileName);

        ///
        /// Resize image of Weight X Height and save
        ///

        public static void ResizeImageAndSave(Stream fs, int intW, int intH, string FileName)
        {
            System.Drawing.Image objImg = Bitmap.FromStream(fs);
            int intImgH = objImg.Height;
            int intImgW = objImg.Width;
            double R = ((double)intImgW / (double)intImgH);
            //if image is greater than 800*800
            if (intImgH > intImgW)
            {
                if (intImgH > intH)
                {
                    intImgH = intH;
                    intImgW = (int)(intH * R);
                }
            }
            else
            {
                if (intImgW > intW)
                {
                    intImgW = intW;
                    intImgH = (int)(intW / R);
                }
            }

          
            Bitmap outputImage = new Bitmap(intImgW, intImgH,PixelFormat.Format24bppRgb);
            outputImage.SetResolution(objImg.HorizontalResolution, objImg.VerticalResolution);
            Graphics graphics = Graphics.FromImage(outputImage);
            graphics.Clear(Color.White);
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(objImg, new Rectangle(0, 0, intImgW, intImgH), new Rectangle(4, 4, objImg.Width - 4, objImg.Height - 4), GraphicsUnit.Pixel);
            graphics.Dispose();
            outputImage.Save(FileName);
            outputImage.Dispose();
            outputImage = null;
        }

Comments

Popular posts from this blog

IIS, There was an error when trying to connect. Do you want to retype your credentials and try again?

IIS 8 error on windows server 2012 insufficient permission There was an error when trying to connect. Do you want to retype your credentials and try again? Details: Filename: \?\C:Windows\system32\inetsrv\config\redirection.config Error: Cannot read configuration file due to insufficient permissions screenshot: Solution: Steps to short out this issue: go to C:\Windows\Microsoft.Net\Framework64\v2.0.50727\CONFIG\machine.config here you got the redirection tag like the below <configurationredirection enabled="true" password="[enc:IISCngProvider:X0ObCWwZ4+PrTHiFVPtzFeCcL8u5P6KUOYfo1/0QrgZWATA5pKWqHvD8nL2crNJKyyqr4z/rBdLPjdRcaLxAMMj4l+lvp5EXXKSXueolvyGa34F4QZfbBVCM6oVNcq3M368TOTVjJv4POVFQWvu0MDVlGgReglXB+Lw5BRI4Htw=:enc]" path="C:\Windows\System32\inetsrv\config\import\" username="Administrator"> </configurationredirection"></li"> you need to change this to <configurationRedirection /> Th...

Plesk - Upgrade the .net Framework to 4.7.2 OR 4.8

 Steps to Upgrade the .net Framework to 4.7.2 OR 4.8 1. First Download the .NET Framework from https://dotnet.microsoft.com/download/dotnet-framework   2. Install on the Server. 3. Login to the Plesk and go to "Tools & Settings" > "Server Components" and refresh the components using the refresh button 4. After update, it will reflect in "Web Script" section at the server component page. All done!!!