Welcome to Hellrocker's Collections

Hope its all of some use to computer users.............

Friday, September 17, 2010

Image resizer in .NET, works for web as well windows apps

Hi all,

Welcome to my next post, have you ever came across the problem of resizing an image to a different size and your code of .Net is resulting in blurry or low quality images. I can bet that if you are in image related website design or development, you must have come across this problem or if not must be finding a way to do it efficiently, because I faced lots of problem while working on my project "http://www.movietadka.com", and collected the following information after searching on Google for whole day.

Well here is the solution...

First we need to have 3 functions, named:

Function 1:
Contains logic for maintaining aspect ratio and quality of image...

resizeImage(Image imgToResize, int newWidth, int newHeight)

Parameters are as follows:

imgToResize: The original image which needs to be resized

newWidth: The desired width for the resultant image

newHeight: The desired height for the resultant image

The Function will look like this:

public static Image resizeImage(Image imgToResize, int newWidth, int newHeight)
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)width / (float)sourceWidth);
nPercentH = ((float)height / (float)sourceHeight);

if (nPercentH < nPercentW)
nPercent = nPercentH;
nPercent = nPercentW;

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);

return (Image)b;

Function 2: Contains logic for saving the image with desired quality at given path

saveJpeg(string path,
Image resultImage, long quality)

Parameters are as follows:

Location to save the new image to

resized image to be saved at path defined above

quality: a long value varying form 0 to 100, depicting the compression ration for image

"Lesser the value, lesser the compression, resulting in higher the quality and higher physical file size and similarly opposite for higher value."

The function will look like this:

void saveJpeg(string path, Image resultImage, long quality)
// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter (System.Drawing.Imaging.Encoder.Quality, quality);

// Jpeg image codec
ImageCodecInfo jpegCodec = this.getEncoderInfo("image/jpeg");

if (jpegCodec == null)

EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;

resultImage.Save(path, jpegCodec, encoderParams);

Function 3: Contains logic for finding the EncoderInformation of required image type (here we are using jpeg)

Parameter as follows:

The type image type for which the Codec Information is to be found ("image/jpeg" is used in our example)

getEncoderInfo(string mimeType)

ImageCodecInfo getEncoderInfo(string mimeType)
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;

To use just call resizeImage function with all parameters and it just works...

Now just go ahead and resize the images in desired sizes and quality.

This is Shashank Sharma, signing off for now. Will be back soon with some more cooked solutions...

Enjoy life


Anonymous said...

High-quality roulette games would possibly be} offered by reputable game developers, they usually allow you to grab loads of exciting bonuses. Best on-line casinos use an algorithm identified as|often identified as} Random Number Generator to randomly resolve the end result} of their games. •Commercial on-line roulette game used and edited to create sensible stimuli. One factor is to make use of a roulette technique just like the Martingale technique, which, nevertheless, does not guarantee you 원 엑스 벳 will at all times win.

Post a Comment