Windows-Like thumbnails in ListView?

Discussion in 'Microsoft C# .NET' started by Vagabond Software, Jul 9, 2005.

  1. 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):

    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 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

    Vagabond Software, Jul 9, 2005
    1. Advertisements

  2. 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 [MVP], Jul 9, 2005
    1. Advertisements

  3. 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.

    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);
    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.

    Vagabond Software, Jul 10, 2005
  4. Try

    grafix.DrawImage(img, xoffset, yoffset, new Rectangle( 0, 0, img.Width,
    img.Height), GraphicUnit.Pixel );
    Alex Feinman [MVP], Jul 10, 2005
  5. Vagabond Software


    Feb 21, 2011
    Likes Received:

    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 ?
    turkush, Feb 21, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.