WinForms: ImageList for ListView with different image sizes

喜夏-厌秋 提交于 2019-12-11 05:14:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!