public Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
// The image has to be atleast somewhat big (Done so as to avoid icons.)
if (loBMP.PhysicalDimension.Height < 200 && loBMP.PhysicalDimension.Width < 200)
return null;
if (loBMP.Height < 150 || loBMP.Width < 150)
{
if ((loBMP.Height < loBMP.Width / 3) || (loBMP.Width < loBMP.Height / 3))
{
return null;
}
}
if ((loBMP.Height < loBMP.Width / 2) || (loBMP.Width < loBMP.Height / 2))
{
return null;
}
// If the image is smaller than a thumbnail just return it
//if (loBMP.Width < 100 && loBMP.Height < 100)
// return loBMP;
if (loBMP.Width.Equals(loBMP.Height))
return null;
lnNewWidth = lnWidth;
lnNewHeight = lnHeight;
// System.Drawing.Image imgOut = loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight, null,IntPtr.Zero);
// This code creates cleaner (though bigger) thumbnails and properly and handles GIF files better by generating a white background for transparent images (as opposed to black)
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch (Exception ex)
{
return null;
}
return bmpOut;
}