private Image MakeThumbNail(Image bmp, ThumbnailOptions options)
{
if (bmp == null)
return null;
int contentWidth;
int contentHeight;
#if !__MonoCS__
int horizontalOffset = 0;
int verticalOffset = 0;
int thumbnailWidth = options.Width;
int thumbnailHeight = options.Height;
#endif
//unfortunately as long as we're using the winform listview, we seem to need to make the icons
//the same size regardless of the book's shape, otherwise the title-captions don't line up.
if (options.CenterImageUsingTransparentPadding)
{
if (bmp.Width > bmp.Height)//landscape
{
contentWidth = options.Width;
contentHeight = (int)(Math.Ceiling(((float)bmp.Height / (float)bmp.Width) * (float)contentWidth));
}
else if (bmp.Width < bmp.Height) //portrait
{
contentHeight = options.Height;
contentWidth = (int) (Math.Ceiling(((float) bmp.Width/(float) bmp.Height)*(float) contentHeight));
}
else //square page
{
contentWidth = options.Width;
contentHeight = options.Height;
}
#if !__MonoCS__
horizontalOffset = (options.Width / 2) - (contentWidth / 2);
verticalOffset = (options.Height / 2) - (contentHeight / 2);
#endif
}
else
{
contentHeight = options.Height;
contentWidth = (int)Math.Floor((float)options.Height * (float)bmp.Width / (float)bmp.Height);
#if !__MonoCS__
thumbnailHeight = contentHeight;
thumbnailWidth = contentWidth;
#endif
}
#if !__MonoCS__
var thumbnail = new Bitmap(thumbnailWidth, thumbnailHeight, PixelFormat.Format64bppPArgb);
using (var graphics = Graphics.FromImage(thumbnail))
{
graphics.PixelOffsetMode = PixelOffsetMode.None;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
var destRect = new Rectangle(horizontalOffset, verticalOffset, contentWidth,contentHeight);
graphics.DrawImage(bmp,
destRect,
0,0, bmp.Width, bmp.Height, //source
GraphicsUnit.Pixel, WhiteToBackground);
if (options.BorderStyle != ThumbnailOptions.BorderStyles.None)
{
using (var pn = new Pen(Color.Black, 1))
{
if (options.BorderStyle == ThumbnailOptions.BorderStyles.Dashed)
{
pn.DashStyle = DashStyle.Dash;
pn.Width = 2;
}
destRect.Height--; //hack, we were losing the bottom
destRect.Width--;
graphics.DrawRectangle(pn, destRect);
}
}
}
return thumbnail;
#else
int skipMarginH = 30;
int skipMarginV = 30;
Bitmap croppedImage = (bmp as Bitmap).Clone(new Rectangle(new Point(skipMarginH, skipMarginV),
new Size(bmp.Width - 2 * skipMarginH, bmp.Height - 2 * skipMarginV)), bmp.PixelFormat);
return croppedImage.GetThumbnailImage(contentWidth, contentHeight, null, IntPtr.Zero);
#endif
}