图像列表,列表视图,图片框

时间:2010-12-28 19:50:51

标签: c#

我想在图片框中显示我的照片。但也希望展示图片的预览。 当用户选择图片时,它会显示在picbox中,但我在重新设置方面存在问题。

这是我的代码

  private void openToolStripMenuItem_Click(object sender, EventArgs e)
{           

            ofd = new OpenFileDialog();
            ofd.Title = "Open an Image File";
            ofd.FileName = "";
            ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
 DirectoryInfo dir = new DirectoryInfo(@"c:\pic");  
foreach (FileInfo file in dir.GetFiles()) 
{
this.imageList1.Images.Add(Image.FromFile(file.FullName));
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(40, 40);
this.listView1.LargeImageList = this.imageList1;

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

                                ListViewItem item2 = new ListViewItem();
                                item2.SubItems.Add(j.ToString());                     



                            }  
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            int i = this.listView1.FocusedItem.Index;

            this.PicBox1.Image = this.imageList1.Images[i];

        }

点击后我只看到(40,40)分辨率的图像我已经设置了它 this.imageList1.ImageSize = new Size(40,40);而不是原始尺寸。 我怎么能拥有它。 2- 我想在每个图像下也写出图像名称和索引(图像号)。 它有可能。 reagrsd,

3 个答案:

答案 0 :(得分:2)

我想,在将图像加载到分辨率为40,40的imageList后,无法将图像加高。

答案 1 :(得分:1)

您应该将原始图片保存在List<>等其他容器中,并显示列表中的原始图片,而不是图片列表中的原始图片。

答案 2 :(得分:0)

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

        }
    }

及其完成。