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

    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
     
    Vagabond Software, Jul 9, 2005
    #1
    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
    ---
    Visit http://www.opennetcf.org
    "Vagabond Software" <> wrote in message
    news:%...
    > 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
    >
     
    Alex Feinman [MVP], Jul 9, 2005
    #2
    1. Advertisements

  3. "Alex Feinman [MVP]" <> wrote in message
    news:O7QD%...
    > 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
     
    Vagabond Software, Jul 10, 2005
    #3
  4. Try

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

    --
    Alex Feinman
    ---
    Visit http://www.opennetcf.org
    "Vagabond Software" <> wrote in message
    news:...
    > "Alex Feinman [MVP]" <> wrote in message
    > news:O7QD%...
    >> 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
    >
     
    Alex Feinman [MVP], Jul 10, 2005
    #4
  5. Vagabond Software

    turkush

    Joined:
    Feb 21, 2011
    Likes Received:
    0
    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
     
    turkush, Feb 21, 2011
    #5
    1. Advertisements

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.
Similar Threads
  1. Rookie
    Replies:
    3
    Views:
    1,041
  2. CaffeineRush

    Copy Listview to Listview

    CaffeineRush, Sep 3, 2004, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    837
    Guest
    Sep 3, 2004
  3. Samuel R. Neff
    Replies:
    0
    Views:
    1,507
    Samuel R. Neff
    Dec 21, 2004
  4. DZ
    Replies:
    1
    Views:
    506
    Nicholas Paldino [.NET/C# MVP]
    May 15, 2006
  5. Jon B

    ListView like Thunderbird

    Jon B, May 23, 2006, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    374
    Ken Tucker [MVP]
    May 24, 2006
  6. Replies:
    2
    Views:
    642
    Ryan Liu
    Jul 8, 2006
  7. forest demon
    Replies:
    4
    Views:
    1,183
    forest demon
    Apr 9, 2007
  8. Def Daemon

    virtual listview with thumbnails

    Def Daemon, Apr 5, 2009, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    4,195
    quietgenie
    Mar 17, 2010
Loading...