Found on StackOverFlow.
http://stackoverflow.com/a/13977707[
^]
public static Bitmap CreateThumbnail(Bitmap source, int thumbWidth, int thumbHeight, bool maintainAspect)
{
if (source.Width < thumbWidth && source.Height < thumbHeight) return source;
Bitmap image = null;
try
{
int width = thumbWidth;
int height = thumbHeight;
if (maintainAspect)
{
if (source.Width > source.Height)
{
width = thumbWidth;
height = (int)(source.Height * ((decimal)thumbWidth / source.Width));
}
else
{
height = thumbHeight;
width = (int)(source.Width * ((decimal)thumbHeight / source.Height));
}
}
image = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(image))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, width, height);
g.DrawImage(source, 0, 0, width, height);
}
return image;
}
catch
{
image = null;
}
finally
{
if (image != null)
{
image.Dispose();
}
}
return null;
}