public Image GetThumbnail(FileInfo from, Size size)
{
try
{
Image image = Bitmap.FromFile(from.ToString());
int width = size.Width;
int height = size.Height;
if (image.Width > image.Height)
height = width * image.Height / image.Width;
else
width = height * image.Width / image.Height;
if (width > size.Width)
{
width = size.Width;
height = width * image.Height / image.Width;
}
if (height > size.Height)
{
height = size.Height;
width = height * image.Width / image.Height;
}
Image returnImage = new Bitmap(size.Width, size.Height);
Graphics graphics = Graphics.FromImage(returnImage);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.High;
graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, returnImage.Width, returnImage.Height);
graphics.DrawImage(image, new Rectangle((size.Width – width) / 2, (size.Height – height) / 2, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
return returnImage;
}
catch (Exception exception)
{
}
return null;
}
To save it, just use image.save(…); with the result