Wednesday, November 19, 2008

Image Editor



Hi All,


Lots of time we used to develop image resizing code (creating thumbnail or resizing image to required height & width), but keeps on forgetting the code, even happen with me too. This code is not actually written by me, but posted here for future references.


This code is posted to keep the code for resizing & sacing an image uploaded.


Resize Image:


Below method shows the code to resize an uploaded image.


Parameters:

  1. (Bitmap imgToResize)[ System.Drawing.Image] – This is an actual image uploaded or provided for resizing.
  2. (Size size) – This is the size of the new resized image.


Code:

  1. The next step is to actually figure out what the size of the resized image should be. The first step is to calculate the percentages of the new size compared to the original. Next we need to decide which percentage is smaller because this is the percent of the original image we will use for both height and width. And now we calculate the number of height and width pixels for the destination image.
  2. The final thing to do is create the bitmap (System.Drawing.Bitmap) which we will draw the resized image on using a Graphics (System.Drawing.Graphics) object. I also set the interpolation mode, which is the algorithm used to resize the image. I prefer HighQualityBicubic, which from my testing seems to return the highest quality results. And just to clean up a little I dispose the Graphics object.


public static Bitmap ResizeImage(Bitmap imgToResize, Size size)

{

int sourceWidth = imgToResize.Width;

int sourceHeight = imgToResize.Height;

float nPercent = 0;

float nPercentW = 0;

float nPercentH = 0;


nPercentW = ((float)size.Width / (float)sourceWidth);

nPercentH = ((float)size.Height / (float)sourceHeight);


if (nPercentH <>

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 (Bitmap)b;

}


Save Image Code:


Below method will save the resized image to the given path.

  1. (string path) – Mapped path for saving image with Image Name.
  2. (Bitmap img) [System.Drawing.Image] – This is an actual image.
  3. (long quality) – This parameter used like number of pixels.


public static void SaveJpeg(string path, Bitmap img, long quality)

{

// Encoder parameter for image quality

EncoderParameter qualityParam =

new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);

// Jpeg image codec

ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");


if (jpegCodec == null)

return;


EncoderParameters encoderParams = new EncoderParameters(1);

encoderParams.Param[0] = qualityParam;

img.Save(path, jpegCodec, encoderParams);

}



private static 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 <> if (codecs[i].MimeType == mimeType)

return codecs[i];

return null;

}



Thanks,

Paresh Bhole

No comments:

Hello

Thanks for checking out my post...

... Paresh Bhole