VagabondSW
Well-known member
- Joined
- Feb 19, 2005
- Messages
- 66
I have a ListView displaying thumbnails created from the original images using the System.Drawing.Image.GetThumbnailImage method.
I implemented some private methods that preserve the image proportions in the thumbnail, just like the Windows Thubmnail view. However, unlike the Windows Thumbnails, my thumbnails are top-justified, rather than centered. Here is a cropped screenshot comparing the Windows Thumbnails (top) to my thumbnails (bottom):
[Broken External Image]:http://home.san.rr.com/vagabondia/images/tmp/sample_v2.gif
In fact, it appears as if my 96h x 96w Bitmap is not part of my image despite my use of the Graphics.DrawImage method. The 96h x 96w dimension is determined by the ImageList.ImageSize height and width, respectively. However, passing those dimensions directly to the Image.GetThumbnailImage method causes non-square images to be distorted. So, I attempt to preserve the aspect ratio of the original image in the thumbnail.
My getAspectRatio method is a simple one line routine that returns a decimal.
The real problem seems to be when I attempt to draw my proportional thumbnail onto a blank Bitmap with the same dimensions as the ImageList.ImageSize. This step does not appear to be happening correctly, or it is not producing the desired Windows-like thumbnail result.
My getOffset method is a simple one line routine that returns an integer.
I thought my preserveAspectRatio routine would draw my proportional thumbnail onto the square bitmap to create a centered proportional image, just like the Windows Thumbnail view. Apparently, there is something Im not doing correctly.
Any help or suggestions would be greatly appreciated.
Code:
System.Drawing.Bitmap thumbnail = getThumbnail(original, imgList.ImageSize.Width, imgList.ImageSize.Height);
[Broken External Image]:http://home.san.rr.com/vagabondia/images/tmp/sample_v2.gif
In fact, it appears as if my 96h x 96w Bitmap is not part of my image despite my use of the Graphics.DrawImage method. The 96h x 96w dimension is determined by the ImageList.ImageSize height and width, respectively. However, passing those dimensions directly to the Image.GetThumbnailImage method causes non-square images to be distorted. So, I attempt to preserve the aspect ratio of the original image in the thumbnail.
Code:
private Bitmap getThumbnail(System.Drawing.Image img, int imgWidth, int imgHeight)
{
decimal aspectRatio = getAspectRatio(img.Height, img.Width);
int thumbWidth = imgWidth;
int thumbHeight = imgHeight;
if (img.Width > img.Height && img.Width != img.Height)
thumbHeight = Convert.ToInt32(imgHeight * aspectRatio);
else
thumbWidth = Convert.ToInt32(imgWidth * aspectRatio);
Image.GetThumbnailImageAbort thumbCallback = new Image.GetThumbnailImageAbort(thumbnailCallback);
Image thumb = (Bitmap) img.GetThumbnailImage(thumbWidth, thumbHeight, thumbCallback, IntPtr.Zero);
return thumb;
}
Code:
private decimal getAspectRatio(int imgHeight, int imgWidth)
{
decimal aspectRatio = (decimal) System.Math.Min(imgHeight, imgWidth) / System.Math.Max(imgHeight, imgWidth);
return aspectRatio;
}
Code:
private Bitmap preserveAspectRatio(int imgHeight, int imgWidth, System.Drawing.Image img)
{
Bitmap bmpImage = new Bitmap(imgWidth, imgHeight);
int xoffset = 0;
int yoffset = 0;
if (img.Width > img.Height)
yoffset = getOffset(bmpImage.Height, img.Width);
else
xoffset = getOffset(bmpImage.Width, img.Height) + img.Height;
Graphics grafix = Graphics.FromImage(bmpImage);
grafix.DrawImage(img, xoffset, yoffset, img.Width, img.Height);
return bmpImage;
}
Code:
private int getOffset(int max, int actual)
{
int offset = (max - actual) / 2;
return offset;
}
Any help or suggestions would be greatly appreciated.