none
Can listView Show Images in diferrent Size?

    Question

  • Hi,
    I got ListView for ImageList.

    Can I show in ListView images is diferent Size? for not destroy the ratio aspect of the Image? vertical Image and horizontal  pictures...

    if So how,

    Thanks...
    Wednesday, July 08, 2009 12:39 PM

Answers

  • Hi gb_007,

    These are my replies to your questions:

    1.    Can I show the ListView images in diferent Sizes and not destroy the ratio aspect of each image?

    Reply: From my experience, it is not possible to achieve your goal if we set the SmallImageList/LargeImageList property to an ImageList and set the image of the ListViewItem by setting the ImageKey/ImageIndex property. It is because in an ImageList, the images would have the same size. However, you can use other structures to store the images to keep the size of each image and draw the icons in the ListView by yourself.

    2.    Can I Create a list<Image> which contains images in different sizes and display the images in the ListView?

    Reply: Yes, but you need to enable owner drawing and draw the icons by yourself.

    This is my solution:

    We can create our own ListView control and enable the owner drawing by setting the OwnerDraw property to true. Then we can override the OnDrawItem methods and draw the image of each ListViewItem by calling the DrawImage method. This is my code snippet:

    public class MyListView : ListView

    {

        public MyListView()

        {

            this.OwnerDraw = true;//Enable custom drawing.

        }

        //Image collection.

        private List<Image> _smallImages = new List<Image>();

        public List<Image> SmallImages { get { return _smallImages; } }

     

        protected override void OnDrawItem(DrawListViewItemEventArgs e)

        {

            base.OnDrawItem(e);           

            if (this.View == View.SmallIcon)

            {

                if (e.Item.ImageIndex < _smallImages.Count)

                {

                    //Draw the image using its own size.

                    Image img = this._smallImages[e.Item.ImageIndex];

                    e.Graphics.DrawImage(img, e.Item.Position.X, e.Item.Position.Y, img.Width, img.Height);

                }

            }

        }

        protected override void Dispose(bool disposing)

        {

            base.Dispose(disposing);

            if (disposing)

            {

                //Dispose the image resources.

                foreach (Image img in this._smallImages)

                    if (img != null)

                        img.Dispose();

            }

        }

    }

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, July 14, 2009 5:53 AM
    Moderator

All replies

  • Or I can Create A list<Image> Which Will contain Images in different Size,

    And Display it in ListView.

    Can I do that?

    Wednesday, July 08, 2009 1:45 PM
  • Still need Help. Thanks...
    Wednesday, July 08, 2009 9:22 PM
  • Hi gb_007,

    These are my replies to your questions:

    1.    Can I show the ListView images in diferent Sizes and not destroy the ratio aspect of each image?

    Reply: From my experience, it is not possible to achieve your goal if we set the SmallImageList/LargeImageList property to an ImageList and set the image of the ListViewItem by setting the ImageKey/ImageIndex property. It is because in an ImageList, the images would have the same size. However, you can use other structures to store the images to keep the size of each image and draw the icons in the ListView by yourself.

    2.    Can I Create a list<Image> which contains images in different sizes and display the images in the ListView?

    Reply: Yes, but you need to enable owner drawing and draw the icons by yourself.

    This is my solution:

    We can create our own ListView control and enable the owner drawing by setting the OwnerDraw property to true. Then we can override the OnDrawItem methods and draw the image of each ListViewItem by calling the DrawImage method. This is my code snippet:

    public class MyListView : ListView

    {

        public MyListView()

        {

            this.OwnerDraw = true;//Enable custom drawing.

        }

        //Image collection.

        private List<Image> _smallImages = new List<Image>();

        public List<Image> SmallImages { get { return _smallImages; } }

     

        protected override void OnDrawItem(DrawListViewItemEventArgs e)

        {

            base.OnDrawItem(e);           

            if (this.View == View.SmallIcon)

            {

                if (e.Item.ImageIndex < _smallImages.Count)

                {

                    //Draw the image using its own size.

                    Image img = this._smallImages[e.Item.ImageIndex];

                    e.Graphics.DrawImage(img, e.Item.Position.X, e.Item.Position.Y, img.Width, img.Height);

                }

            }

        }

        protected override void Dispose(bool disposing)

        {

            base.Dispose(disposing);

            if (disposing)

            {

                //Dispose the image resources.

                foreach (Image img in this._smallImages)

                    if (img != null)

                        img.Dispose();

            }

        }

    }

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, July 14, 2009 5:53 AM
    Moderator