By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,160 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,160 IT Pros & Developers. It's quick & easy.

Resize an image and save it to the target folder

P: 1
TO Do
I get an image and I have to make sure that the image is 1024x768 in height and width and then save it.

when I take the Image and save it its size is becoming huge.and is not opening.

Here is the code for that.

Expand|Select|Wrap|Line Numbers
  1. Image resized = ResizeImageForFull(original, new Size(768, 1024));
  2.  
  3.             //save the image to memory stream
  4.             MemoryStream memStream = new MemoryStream();
  5.             resized.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  6.  
  7. public static Image ResizeImageForFull(Image image, Size size, bool preserveAspectRatio = true)
  8.         {
  9.             int newWidth=0;
  10.             int newHeight=0;
  11.  
  12.             if (preserveAspectRatio)
  13.             {
  14.                 newWidth = size.Width;
  15.                 newHeight = size.Height;
  16.             } 
  17.  
  18.             Image newImage = new Bitmap(newWidth, newHeight);
  19.             using (Graphics graphicsHandle = Graphics.FromImage(newImage))
  20.             {
  21.                 graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
  22.                 graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
  23.             }          
  24.  
  25.             return newImage;
  26.         }
Nov 14 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.