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...

Wordpress - Contact Form 7 notification open in popup - Bootstrap modal popup

How to add open Contact form 7 Notification / Messages in Modal popup We can use the Bootstrap modal popup and Contact form 7 Doom Events to create the popup. Paste the below code in the functions.php, it will work for you (please change the messages as per the requirements): add_action('wp_footer', 'munesh_Footertext'); function munesh_Footertext() { echo '<div class="modal fade in formids" id="myModal2" role="dialog" tabindex="-1">     <div class="modal-dialog">       <!-- Modal content-->       <div class="modal-content no_pad text-center">          <button type="button" class="close" data-dismiss="modal">&times;</button>         <div class="modal-header heading">           <h3 class="modal-title" id="MSTitle">Message Sent!</b></h3>         </div>      ...