【问题标题】:List view control displaying distorted images显示扭曲图像的列表视图控件
【发布时间】:2009-10-13 19:32:53
【问题描述】:

Windows 窗体应用程序中的 ListView 控件有问题。 即使我创建了缩略图图像或调整了真实图像的大小,我也会在列表视图中得到扭曲的图像。 当您放大图像时,图像看起来像。 我首先认为 GetThumbnailImage 正在解决这个问题,但我使用了我在这里找到的调整大小代码,我得到了相同的结果。

我也没有发现任何与列表视图控件相关的错误,所以我猜我做错了什么,但我就是不知道是什么。 这是我使用的代码:

lsvPictures.LargeImageList = m_imagesList;
lsvPictures.LargeImageList.ImageSize = new Size(100, 100);
lsvPictures.View = View.LargeIcon;
lsvPictures.CheckBoxes = true;
for (int i = 0; i < ofd.FileNames.Length; i++)
{
    filename = ofd.FileNames[i].ToString();    
    ListViewItem lvi = new ListViewItem(filename);
    m_imagesList.Images.Add(ResizeImage(Image.FromFile(filename), 100, 100));
    lvi.ImageIndex = i;
    lsvPictures.Items.Add(lvi);
} 

这是调整图像大小的函数:

public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, 
                                                int width, int height)
{        
    //a holder for the result
    Bitmap result = new Bitmap(width, height);

    //use a graphics object to draw the resized image into the bitmap
    using (Graphics graphics = Graphics.FromImage(result))
    {
        //set the resize quality modes to high quality
        graphics.CompositingQuality = 
            System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.InterpolationMode =
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = 
            System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //draw the image into the target bitmap
        graphics.DrawImage(image, 0, 0, result.Width, result.Height);
    }
    //return the resulting bitmap
    return result;
}

谢谢! 摩苏'

我刚刚找到问题的根源:

m_imagesList.ColorDepth = ColorDepth.Depth16Bit;

默认情况下,ImageList 的 ColorDepth 是 8 位(或 4 位,但我猜是 8)。如果我将其更改为至少 16 位,一切看起来都非常好。

对于那些有类似问题的人:在我意识到 ListView 控件没有使用图像所具有的颜色深度之前,我对 Thumbnail 方法进行了很多更改。我将我的方法的结果放在 PictureBox 控件上,并看到该函数正常工作。 Atfer 这个我用谷歌搜索了很多......并发现了那个愚蠢的 ColorDepth 属性。

【问题讨论】:

    标签: c# list view


    【解决方案1】:

    您如何设置图像的分辨率。另外,您在创建位图时将 PixelFormat 值设置为什么?我有一个图像列表加载到我的列表视图中,我正在调整大小,类似于你的大小,它工作正常,在创建的缩略图图像中没有任何失真。

    这是我的 resize 方法中的一个 sn-p。

    Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.Red);
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(image,
                    new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight),
                    new Rectangle(sourceX, sourceY, originalWidth, originalHeight),
                    GraphicsUnit.Pixel);
            }
            return bitmap;
    

    【讨论】:

    • 感谢您的回答。我将方法更改为使用 SetResolution 方法,但这并没有提高我的图像质量。我还使用了不同的 Interpolation 和 PixelFormat 常量,但无济于事。
    【解决方案2】:

    我也在 WinForms 中使用 ListView 来显示目录,并且遇到了同样的问题。我建议您检查图像文件类型:图标文件 (.ico) 往往会失真,因此请尝试使用扩展名为 .png 的图像文件。这对我有用:

    ListView listView = new ListView();
    ImageList imageList = new ImageList();
    // add image to list:
    imageList.Images.Add("image_key", image_path);
    // give the listview the imagelist:
    listView.SmallImageList = imageList;
    // add item to listview:
    listView.Items.Add("item_text", "image_key");
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2012-05-31
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2014-04-08
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多