Create thumbnail of a Image a given image

Lakhangarg
Posted by Lakhangarg under C# category on | Views : 1929
Suppose we have a large image and want to create a thumbnail from that image.

Sample Code:


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;
}
}


Now to get the thumbnail of given image you need to pass image name with physical path and height and width of the required image.

Thanks & Regards
Lakhan Pal Garg

Comments or Responses

Login to post response