简体   繁体   English

WinForms:具有不同图像大小的ListView的ImageList

[英]WinForms: ImageList for ListView with different image sizes

I have a ListView with View property set to LargeIcon . 我有一个View属性设置为LargeIconListView And I have an ImageList as image source for ListView . 我有一个ImageList作为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. 我要的是显示在垂直和水平方向图像ListViewImageList只有一个属性ImageSize所有图像在其收集如此,例如,如果我设置该属性为150x100的垂直影像(100x150)加至收集- ListView自动将其拉伸到150x100。

So as I understand I need some ImageList , in which every image is stored with its original size. 因此,据我了解,我需要一些ImageList ,其中每个图像ImageList其原始大小存储。 Any thoughts about how to do that? 关于如何做的任何想法? Thanks in advance. 提前致谢。

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. 然后,在创建新的位图时,可以使用所需的大小和派生的大小在新的位图上绘制缩放图像。

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. 我自己遇到了这个问题,这是我的工作,希望对您有所帮助,首先确定要使用的最大图像尺寸(例如200x200),然后使用具有透明背景的Png或Gif图像(所有200x200)。

have a look at these two images i have created as an example. 看一下我创建的这两个图像作为示例。

这是原始的64x64图像文件

but I make it like this to avoid stretching: 但我这样做是为了避免拉伸:

这是200x200的图片文件,具有很多透明区域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM