tags:

views:

272

answers:

1

Hi,

I have a problem with the ListView control in a windows forms application. Even if I create a thumbnail image or resize the real one I get distorted images in the list view. The image looks like when you zoom in an image very much. I first thought that the GetThumbnailImage is couseing this but I used a resize code I found here and I have the same result.

I also did not found any bug related to list view control so I gues I'm doing something wrong but I just can't figure out what. Here is the code I use:

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);
}

And this is the function that resizes images:

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;
}

Thank you! Mosu'

I just found the source of the problems:

m_imagesList.ColorDepth = ColorDepth.Depth16Bit;

It seams that, as default, the ColorDepth of the ImageList is 8 bit (or 4 bit, but my guess is 8). If I change this to at least 16 bit everything looks very nice.

To those with similar problems: I changed my Thumbnail method a lot before I realised that the ListView control is not using the color depth the images were having. I put the result of my method on a PictureBox control and saw that the function was working corectly. Atfer this I googled a lot ... and found that silly ColorDepth property.

A: 

How did you set the resolution for your image. Also, did what did you set the PixelFormat value to when you created the bitmap? I have a list of images loading into my list view that I am resizing similar to how you are and it is working fine without any distortion in the resulting thumbnail images that are created.

Here is a snippet from my resize method.

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;
Wil P
Thank you for your anwser.I change my method to use the SetResolution method but this didn't improve my images quality. I also played with different Interpolation and PixelFormat constants but to no avail.
mosu