WinForms: ImageList for ListView with different im

2019-07-27 12:58发布

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.

2条回答
贪生不怕死
2楼-- · 2019-07-27 13:40

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.

查看更多
做自己的国王
3楼-- · 2019-07-27 13:57

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.

this is the original 64x64 image file

but I make it like this to avoid stretching:

this is the 200x200 image file that have lots of transparent area

查看更多
登录 后发表回答