简体   繁体   中英

display image from listview to picturebox

I want to display image from list to picturebox. My image is displaying in the picturebox but the problem is it shows the size the of the image in the picturebox that is defined in the list. Can anyone tell me how to enlarge my image size?

here is my piece of code:

private void listView_SelectedIndexChanged(object sender, EventArgs e)
    {
           foreach (ListViewItem itm in listView.SelectedItems)
        {
            int imgIndex = itm.ImageIndex;
            if (imgIndex >= 0 && imgIndex < this.documents.Images.Count)
            {
               // this.documents.Images[imgIndex].Width = 417;

                pictureBox.Image = this.documents.Images[imgIndex];
            }
        } 
    }

and this is how I am getting images from the database:

ImageList documents = new ImageList();

if (documents.Images.Count < 1)
        {
            MessageBox.Show("No Documents Found.");
        }
        else
        {
         //   pictureBox.Image = documents.Images[1];
            this.listView.View = View.LargeIcon;
            documents.ImageSize = new Size(256, 256);
            listView.LargeImageList = documents;

            listView.Items.Clear();

            for (int j = 0; j < documents.Images.Count; j++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = j;
                this.listView.Items.Add(item);
            }  
        }

PictureBox has SizeMode property that is used for manipulating image size:

pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;

More about that on MSDN

 -Create a new imagelist (imagelist1)**
 -Add images to your imagelist 
 -Create a new listview (listview1)
 -Create a picturebox (picturebox1)
 -Create a new button (button1)
 -Create another button (button2)**

  -Import  images from imagelist1 to listview1

private void button1_Click(object sender, EventArgs e)
{
    listView1.Scrollable = true;
    listView1.View = View.LargeIcon;
    imageList1.ImageSize = new Size(100, 100);
    listView1.LargeImageList = imagelist1;

    for (int i = 0; i < imagelist1.Images.Count; ++i)
    {
        string s = imagelist1.Images.Keys[i].ToString();
        ListViewItem lstItem = new ListViewItem();
        lstItem.ImageIndex = i;
        lstItem.Text = s;
        listView1.Items.Add(lstItem);
    }
}

 - Set the selected image into your picture box from listview

  private void button2_Click(object sender, EventArgs e)
{
    if (this != null && listView1.SelectedItems.Count > 0)
    {
        ListViewItem lvi = listView1.SelectedItems[0];
        string imagekeyname = lvi.Text;

        if (this.pictureBox1.Image != null)
        {
            this.pictureBox1.Image.Dispose();
            this.pictureBox1.Image = null;
        }

        //set the selected image into your picturebox
        this.pictureBox1.Image = imagelist1.Images[imagekeyname];

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM