将图像从列表视图显示到图片框

本文关键字:显示 视图 图像 列表 | 更新日期: 2023-09-27 18:21:20

我想显示从列表到图片框的图像。我的图像显示在图片框中,但问题是它显示了列表中定义的图片框中图像的大小。有人能告诉我如何放大我的图像尺寸吗?

这是我的代码:

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

这就是我从数据库中获取图像的方式:

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具有用于操纵图像大小的SizeMode属性:

pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;

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