how to keep image proportions in imagelist

T

Tim Mackey

hi,
i have a listview with an associated imagelist. the imagelist is set to
45x45 pixels, but most of the images aren't square, so they get skewed to
fill the 45x45 space.
is there an easy and efficient way to maintain the aspect of the images?
i.e. create a new blank 45x45 bitmap and overlay the rectangular image on
it? i could do a hack but i would prefer to get an experts opinion before i
go re-inventing the wheel!

many thanks
tim
 
H

Herfried K. Wagner [MVP]

* "Tim Mackey said:
i have a listview with an associated imagelist. the imagelist is set to
45x45 pixels, but most of the images aren't square, so they get skewed to
fill the 45x45 space.
is there an easy and efficient way to maintain the aspect of the images?
i.e. create a new blank 45x45 bitmap and overlay the rectangular image on
it? i could do a hack but i would prefer to get an experts opinion before i
go re-inventing the wheel!

Is it really necessary to use an imagelist?
 
T

Tim Mackey

hi Herfried,
thanks for the reply. what i am doing is using a listview to display
thumbnail images, with a title underneath them. user can double-click an
item to view full size etc. i assumed the large-icon display mode of the
listview would suit me best here. i couldn't find a different way to set an
image for a listviewitem.
thanks for any tips
tim
 
Y

Ying-Shen Yu[MSFT]

Hi Tim,

Imagelist does not have the ability to scale images, you need scale it
manually before adding it into the Imagelist. The Image.GetThumbnailImage
method will help you getting a thumbnail for an image, you may read the
document for that API for more information:

<Image.GetThumbnailImage Method>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdrawingimageclassgetthumbnailimagetopic.asp

If you have any questions on this issue, please feel free to post in this
thread.

Have a nice day!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
T

Tim Mackey

Hi Ying-Shen,
Thanks for the reply. I think i might not have made my situation clear.
I already have the thumbnail images, and the problem arises where my image
is rectangular, and it gets stretched to fit the square size of the
dimensions i have set in the imagelist. ideally, i would like to add
transparent space around the rectangular image to make it fit into a square
shape (for the imagelist) but preserve the proportion of the original image.
i already have the calculations to preserve the aspect of the image when
resizing, but i don't know how to put a rectangular image into a square
blank image, and leave the gaps transparent.

thanks for any help
tim
 
Y

Ying-Shen Yu[MSFT]

Hi Tim,

So your question is how to put a rectangular image into a square blank
image, and leave the gaps transparent.
You may create a new Bitmap object and draw your thumbnail image onto the
blank bitmap.
Here is a sample snippet for how to draw thumbnail on a blank bitmap:
<code>
//Load image file
Image img = Image.FromFile(@"image.jpg");

//calculate the scale ratio and offset to draw the thumbnail onto the
blankbitmap
...

//Create a blank bitmap using the image size of the large imagelist
using (Bitmap bmp = new Bitmap(largeImageList.ImageSize.Width,
largeImageList.ImageSize.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
//DrawImage using the calculated offset and
the scaled sized
g.DrawImage(img,offsetX,offsetY,scaledWidth,scaledHeight);
}
largeImageList.Images.Add(bmp);

//set the imagelist to listview, this should be finished
before the bitmap disposed, since the imagelist will use
lazy-initialization
// it will delay creating the underlying ImageList object
until really needs.

if (listView1.LargeImageList == null)
listView1.LargeImageList = largeImageList;
}
</code>

Does it resolve your problem?
If you have anything unclear about my reply, please feel free to reply this
thread to let me know.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top