Create Thumbnail

This Code will help the users to create thumbnail of an image. for this user need to pass three parameters as:
(1) Image To Scale
(2) Desired height of the output image
(3) Desired width of output image

First we will get the image in a Bitmap Object and then we will scale the image.

Here is the Code to create thumbnail:

/// Scales an image as per the ratio of dimensions of the image to hight and width specified in the
/// parameters

public static System.Drawing.Image ScaleByPercent(string strImage, double dblImgHt, double dblImgWd)
{
Bitmap imgRetPhoto = null;
double dblWdRatio, dblHtRatio;

try
{
imgRetPhoto = new Bitmap(strImage);
if (imgRetPhoto.Height > Convert.ToInt32(dblImgHt) || imgRetPhoto.Width > Convert.ToInt32(dblImgWd))
{
if (imgRetPhoto.Height > dblImgHt)
{
dblHtRatio = dblImgHt / Convert.ToDouble(imgRetPhoto.Height);
dblWdRatio = Convert.ToDouble(imgRetPhoto.Width) * dblHtRatio;
imgRetPhoto = new Bitmap(imgRetPhoto, Convert.ToInt32(dblWdRatio), Convert.ToInt32(dblImgHt));
imgRetPhoto.SetResolution(imgRetPhoto.HorizontalResolution, imgRetPhoto.VerticalResolution);
}

if (imgRetPhoto.Width > dblImgWd)
{
dblWdRatio = dblImgWd / Convert.ToDouble(imgRetPhoto.Width);
dblHtRatio = Convert.ToDouble(imgRetPhoto.Height) * dblWdRatio;
imgRetPhoto = new Bitmap(imgRetPhoto, Convert.ToInt32(dblImgWd), Convert.ToInt32(dblHtRatio));
imgRetPhoto.SetResolution(imgRetPhoto.HorizontalResolution, imgRetPhoto.VerticalResolution);
}
return imgRetPhoto;
}
else
return imgRetPhoto;
}
catch (Exception ex)
{
throw ex;
}
}

0 comments: