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;
else
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);
g.Dispose();

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:

path:
Location to save the new image to

resultImage:
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)
return;

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:

mimeType:
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

0 comments:

Post a Comment