Image resizing is widely used in web and windows application. In dot net we have System.Drawing.Image class which can be used for image resizing at run time (code behind). To better understand these please go through the below information:
- Create a new Console Application.
- Add a class named ImageResizer.cs and create a method ResizeImage().
- In Main method of Program.cs call the method ResizeImage() by passing the appropriate parameter.
Code snippet of ImageResizer.cs class
/// <summary>
/// class contains the method which resize the image
/// </summary>
public class ImageResizer
{
/// <summary>
/// method use to resize the image at run time.
/// </summary>
/// <param name="OriginalFile">orginal image file path</param>
/// <param name="NewFile">target image file path</param>
/// <param name="NewWidth">new resized width of image</param>
/// <param name="MaxHeight">new resized height of image</param>
/// <param name="OnlyResizeIfWider">true/false if true than it will resize the image if the original width is higher than the new width</param>
public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
// Prevent
using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if
(OnlyResizeIfWider)
{
{
NewWidth = FullsizeImage.Width;
}
}
int
NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = FullsizeImage.Width * MaxHeight /
FullsizeImage.Height;
NewHeight = MaxHeight;
}
System.Drawing.Image NewImage =
FullsizeImage.GetThumbnailImage(NewWidth, NewHeight,
null, IntPtr.Zero);
// Clear
handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save
resized picture
NewImage.Save(NewFile);
}
}
Code snippet of Main method (Program.cs)
static
void Main(string[]
args)
{
ImageResizer imgRe =
new ImageResizer();
string originalImageFilePath =
System.Text.RegularExpressions.Regex.Replace(Environment.CurrentDirectory,
@"\\([^\\])+\\([^\\])+(\\)?$",
"") + "\\sampleImg\\1_300_300.jpg";
string newImageFilePath =
System.Text.RegularExpressions.Regex.Replace(Environment.CurrentDirectory,
@"\\([^\\])+\\([^\\])+(\\)?$",
"") + "\\sampleImg\\"
+ DateTime.Now.ToShortDateString().Replace("/",
"").Replace("-",
"") + "_"
+ DateTime.Now.Ticks.ToString() +
".jpg";
imgRe.ResizeImage(originalImageFilePath, newImageFilePath, 155, 155, true);
Console.WriteLine("Press
any Key to exit");
Console.ReadLine();
viral vasaiwalaPosted Jun 25, 2010, 11:38 AM
Hi Sam, Quality can not be bad with this code. because Image is being resized by maintaining it's aspect ratio. Where MaxImage height is being considered. and according to maxHeight and original height, width is calculated and set. I have already used this code for image gallery application and it works as it should work. There might be some other things wrong in the code. Please check it again.
SamPosted Jun 25, 2010, 2:41 AM
Image quolity of .GetThumbnailImage( is too bad
viral vasaiwalaPosted Jun 24, 2010, 11:51 AM
Thanks a lot.