Windows-Like thumbnails in ListView?

  • Thread starter Thread starter Vagabond Software
  • Start date Start date
V

Vagabond Software

I'm trying to display thumbnail images in a Listview that look more like the
Windows thumbnail view. Everything is working pretty good, but my
thumbnails are decidedly not like the Windows thumbnails.

View the following snapshot to compare the Windows thumbnails (top) to my
ListView thumbnails (bottom):

http://home.san.rr.com/vagabondia/images/tmp/sample.gif

It looks like the Windows thumbnails retain their proportion by, perhaps,
using some kind of "spacer" image on the top and bottom to create the
"letterbox" effect. I try and maintain the aspect ratio of the original
image, but it stretches into the height and width determined by the
ImageList. Here is my code to get thumbnails from the original images:

--- code ---
thumbnail = getThumbnail(originalImage, imglstStock.ImageSize.Width);

private Image getThumbnail(System.Drawing.Image image, int desiredWidth)
{
int imgWidth = image.Width;
int imgHeight = image.Height;
decimal ratio = (decimal) desiredWidth / imgWidth;
int desiredHeight = Convert.ToInt32(ratio * imgHeight);

Image.GetThumbnailImageAbort thumbCallback = new
Image.GetThumbnailImageAbort(thumbnailCallback);
Image thumb = image.GetThumbnailImage(desiredWidth, desiredHeight,
thumbCallback, IntPtr.Zero);
return thumb;
}

private bool thumbnailCallback()
{
return false;
}
--- code end ---

Any suggestions, advice, or links to reading material would be greatly
appreciated.

Carl
 
You can check the Height/Width of the image returned by getThumbnail
and I bet you'll see that the aspect ratio is correct. What happens is
that Listview stretches your image. Modify your function getThumbnail
to create a square bitmap of the listview thumbnail size, clear it with
white and draw your thumbnail onto it.

One other thing you need to do is to handle vertical images, i.e. those
where height is greater than width
 
Alex Feinman said:
You can check the Height/Width of the image returned by getThumbnail
and I bet you'll see that the aspect ratio is correct. What happens is
that Listview stretches your image. Modify your function getThumbnail
to create a square bitmap of the listview thumbnail size, clear it with
white and draw your thumbnail onto it.

One other thing you need to do is to handle vertical images, i.e. those
where height is greater than width

--

Alex, thanks for the reply. Your suggestions were very helpful and things
are much improved. As you can see in the following sample_v2 screeshot, my
thumbnail images are now identical to the Windows thumbnail images.

http://home.san.rr.com/vagabondia/images/tmp/sample_v2.gif

However, you'll also see that my images are top-justified, which is not what
I wanted. I want them centered, just like the Windows thumbnails. Here is
the code where I try to paint my thumbnail onto a square bitmap image:

--- code ---
/* Height and Width are those of the ImageList.ImageSize.
* Image is the thumbnail being painted onto the background.
*/
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;

using(Graphics grafix = Graphics.FromImage(bmpImage))
{
grafix.DrawImage(img, xoffset, yoffset, img.Width, img.Height);
}
return bmpImage;
}
--- code end ---

Your help and advice is greatly appreciated.

Carl
 
Try

grafix.DrawImage(img, xoffset, yoffset, new Rectangle( 0, 0, img.Width,
img.Height), GraphicUnit.Pixel );
 
Hi,

I had same prob- my images in image List are being streched. Im now getting the thumbnails but the still same streching is happening. Infact i dont understand how to "Clear it with White".

its urgent any idea ?
Thanks
 
Back
Top