问题
I have a ListView
with View
property set to LargeIcon
. And I have an ImageList
as image source for ListView
.
What I want is to display both vertically and horizontally oriented images in ListView
but ImageList
have only a single property ImageSize
for all images in its collection so for example if I set that property to 150x100 and add a vertical image (100x150) to collection - ListView
automatically stretches it to 150x100.
So as I understand I need some ImageList
, in which every image is stored with its original size. Any thoughts about how to do that?
Thanks in advance.
回答1:
I have had this problem myself, Here is what I did, I hope it will help you as well, first determine the biggest image size you would use ( for example 200x200) then use Png or Gif images (all 200x200) with transparent backgrounds.
have a look at these two images i have created as an example.
but I make it like this to avoid stretching:
回答2:
To keep from stretching you can use cross-multiplication.
Image Width Desired Width
--------------- = ---------------
Image Height New Height
or
Image Width New Width
--------------- = ---------------
Image Height Desired Height
In code it looks something like this
int height = img.Width*newWidth/image.Height;
or
int height = img.Width/image.Height*newWidth;
and
int width = img.Height*newHeight/image.Width;
or
int width = img.Height/image.Width*newHeight;
Then when creating your new bitmap you can draw a scaled image on the new bitmap using the desired size and the derived size.
来源:https://stackoverflow.com/questions/8365366/winforms-imagelist-for-listview-with-different-image-sizes