How do I add images into a list view on C# Windows Form?

614 views Asked by At

I am trying to create a windows form on .NET framwork (C#) that shows the list of images o( Photo Gallery) of all users on my list. I tried to code it but it doesn't show any image, just the label or name.

These set of code is what I have so far, on formload event on my project.

private void PicGallery_Load(object sender, EventArgs e)
{
    ShowStudentImages();
}

private void ShowStudentImages()
{
    foreach (Students student in _studentList)
    {
        // Skapa rätt storlek för bilderna - se img control i design
        Image thumbLarge = GetThumb(student.PictureStudents, 128);
        Image thumbSmall = GetThumb(student.PictureStudents, 32);

        // Lägg till bilden i listorna med unik personnummer
        imgGalleryLarge.Images.Add(student.PersonNumberStudents, thumbLarge);
        imgGallerySmall.Images.Add(student.PersonNumberStudents, thumbSmall);

        ListViewItem lvi = new ListViewItem(student.LastnameStudents + " " +
                student.FirstnameStudents);
        lvi.SubItems.Add(student.PhoneStudents);
        lvi.SubItems.Add(student.EmailStudents);
        lsvImageGallery.Items.Add(lvi);
    }
}

private Bitmap GetThumb(Image image, int maxSize)
{
    int tWidth, tHeight, tExtraX, tExtraY;
    int width = image.Width;
    int height = image.Height;

    double whRatio = (double)width / height;

    if (image.Width >= image.Height)
    {
        tWidth = maxSize;
        tHeight = (int)(tWidth / whRatio);
    }
    else
    {
        tHeight = maxSize;
        tWidth = (int)(tHeight * whRatio);
    }

    tExtraX = (maxSize - tWidth) / 2;
    tExtraY = (maxSize - tHeight) / 2;

    Bitmap imgthumb = new Bitmap(maxSize, maxSize, PixelFormat.Format24bppRgb);
    Graphics imggraphics = Graphics.FromImage(imgthumb);
    imggraphics.Clear(Color.White);
    imggraphics.InterpolationMode = InterpolationMode.NearestNeighbor;
    imggraphics.DrawImage(image, new Rectangle(tExtraX, tExtraY, tWidth, tHeight), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);

    return imgthumb;
}
1

There are 1 answers

3
King King On

Try this:

int i = 0;
foreach (Students student in _studentList) {

   // Skapa rätt storlek för bilderna - se img control i design
   Image thumbLarge = GetThumb(student.PictureStudents, 128);
   Image thumbSmall = GetThumb(student.PictureStudents, 32);


   // Lägg till bilden i listorna med unik personnummer

   imgGalleryLarge.Images.Add(student.PersonNumberStudents, thumbLarge);
   imgGallerySmall.Images.Add(student.PersonNumberStudents, thumbSmall);

   ListViewItem lvi = new ListViewItem(student.LastnameStudents + " " +
                                       student.FirstnameStudents, i++);
   lvi.SubItems.Add(student.PhoneStudents);
   lvi.SubItems.Add(student.EmailStudents);
   lsvImageGallery.Items.Add(lvi);

}

You have to set the ImageIndex for each ListViewItem, the constructor for ListViewItem used above is:

public ListViewItem(string text, int imageIndex);