Här är en gammal kod som jag har använt. CFile är ett egendefinierat filobjekt. Den här koden skapar en thumbnail med en viss storlek med vit bakgrund och fyller bilden i rektangeln. Hoppas du kan använda den på något sätt. :)
/// <summary>
/// Creates a thumbnail for a given CFile object.
/// </summary>
/// <param name="oFile">The CFile object to create thumbnail for.</param>
/// <param name="strDestPath">The destination path, including file name, where to put it.</param>
public void GenerateThumbNail(CFile oFile, string strDestPath)
{
Bitmap oImg;
ImageFormat oFormat;
if (oFile.IsAnImageFile())
{
CImageFile oImageFile = (CImageFile) oFile;
oImg = oImageFile.oBitmap;
oFormat = oImageFile.ImageFormat;
}
else
{
oImg = CShell.GetFileIcon(oFile.strPath);
oFormat = ImageFormat.Gif;
}
Image oThumbNail = new Bitmap(oImg, this.nThumbnailWidth, this.nThumbnailHeight);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
oGraphic.FillRectangle(new SolidBrush(Color.White), -2, -2, oThumbNail.Size.Width+2, oThumbNail.Size.Height+2);
Size newSize = ImageVariantSetting.GetCalculatedSize(oImg.Size, this.nThumbnailWidth, this.nThumbnailHeight);
int x = (newSize.Width < oThumbNail.Size.Width) ? ((oThumbNail.Size.Width - newSize.Width) / 2) : 0;
int y = (newSize.Height < oThumbNail.Size.Height) ? ((oThumbNail.Size.Height - newSize.Height) / 2) : 0;
Rectangle oRectangle = new Rectangle(x, y, newSize.Width, newSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
oThumbNail.Save(strDestPath, oFormat);
}

