【问题标题】:ListView Larget Icon vertical alignment of the images图像的 ListView 大图标垂直对齐
【发布时间】:2012-08-17 19:22:23
【问题描述】:

我有 View 类型为 LargeIcon 的 ListView。 ListView 已分配 LargeImageList。分配的 ImageList 的 ImageSize 为 200x200。 将添加到 ImageList 的图像的大小与 200x200 ImageList 大小不匹配。它们可以具有较小的宽度或高度。在这两种情况下,我都希望图像按中心对齐,即 Winforms.Label 类的 MiddleCenter 属性

【问题讨论】:

    标签: c# .net winforms listview imagelist


    【解决方案1】:

    ImageList 将调整图像大小以适应 ImageSize。要使图像保持原始大小并居中,您需要创建具有所需属性的新图像。执行此操作的示例代码(未经测试):

    public static void AddCenteredImage(ImageList list, Image image) {
        using (var bmp = new Bitmap(list.ImageSize.Width, list.ImageSize.Height))
        using (var gr = Graphics.FromImage(bmp)) {
            gr.Clear(Color.Transparent);   // Change background if necessary
            var size = image.Size;
            if (size.Width > list.ImageSize.Width || size.Height > list.ImageSize.Height) {
                // Image too large, rescale to fit the image list
                double wratio = list.ImageSize.Width / size.Width;
                double hratio = list.ImageSize.Height / size.Height;
                double ratio = Math.Min(wratio, hratio);
                size = new Size((int)(ratio * size.Width), (int)(ratio * size.Height));
            }
            var rc = new Rectangle(
                (list.ImageSize.Width - size.Width) / 2,
                (list.ImageSize.Height - size.Height) / 2,
                size.Width, size.Height);
            gr.DrawImage(image, rc);
            list.Images.Add(bmp);
        }
    }
    

    【讨论】:

    • 感谢您的代码。我预计我需要您发布的类似内容。我认为必须有另一种方法来垂直对齐图像。不过貌似没有别的办法,只能自己做.NET Framework的工作
    猜你喜欢
    • 2010-12-03
    • 2022-12-23
    • 1970-01-01
    • 2016-09-06
    • 2019-05-20
    • 2014-11-30
    相关资源
    最近更新 更多