Bitmap bmp; using(Bitmap temp = System.Drawing.Image.FromStream(fuInsertImage1.PostedFile.InputStream)) bmp = resize_image(temp); bmp.Save(directory); bmp.Dispose(); protected Bitmap resize_image(Bitmap img) { //determines if image width is larger that 1024 pixels //if yes, resizes image to 1024 width and keeps aspect ratio for height. int width, height; float percentage = 1; //This is to keep the aspect ratio of the image width = img.Width; height = img.Height; if (width > 1024) { percentage = (float)1024 / width; Bitmap bmp = new Bitmap((int)Math.Round(width * percentage, 0), (int)Math.Round(height * percentage, 0)); Graphics graphic = Graphics.FromImage((System.Drawing.Image)bmp); try { graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphic.DrawImage(img, 0, 0, (int)Math.Round(width * percentage), (int)Math.Round(height * percentage)); //This is where the exception is thrown. return bmp; } catch (Exception ex) { return null; } finally { bmp.Dispose(); graphic.Dispose(); } } else { return img; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)