从ListBox中选择并显示要在PictureBox中显示的图像文件?C#

本文关键字:显示 图像 PictureBox 文件 ListBox 选择 | 更新日期: 2023-09-27 17:58:33

我正在尝试使我的listBox(lstFiles)可供选择,这样它就可以在pictureBox(
pictureBox1。

我已经尝试了很多方法,但无法从列表框中选择文件名

任何帮助都将不胜感激,谢谢

这就是我目前所拥有的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Pinvoke;
using System.IO;
namespace TestAvicap32
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeDevicesList();
        }
        private void InitializeDevicesList()
        {
            splitContainer1.Panel1.Enabled = true;
            splitContainer1.Panel2.Enabled = false;
            foreach (CaptureDevice device in CaptureDevice.GetDevices())
            {
                cboDevices.Items.Add(device);
            }
            if (cboDevices.Items.Count > 0)
            {
                cboDevices.SelectedIndex = 0;
            }
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            int index = cboDevices.SelectedIndex;
            if (index != -1)
            {
                splitContainer1.Panel1.Enabled = false;
                splitContainer1.Panel2.Enabled = true;
                ((CaptureDevice)cboDevices.SelectedItem).Attach(pbImage);
            }
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel1.Enabled = true;
            splitContainer1.Panel2.Enabled = false;
            ((CaptureDevice)cboDevices.SelectedItem).Detach();
        }
        private void btnSnapshot_Click(object sender, EventArgs e)
        {
            try
            {
                Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
                image.Save(@"c:'webcapture'" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }   
        }
        private void btntimer_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            btntimerstop.Visible = true;
            btntimer.Visible = false;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
            image.Save(@"c:'webcapture'" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
        }
        private void btntimerstop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            btntimer.Visible = true;
            btntimerstop.Visible = false;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            btntimerstop.Visible = false;
            foreach (DriveInfo di in DriveInfo.GetDrives())
                lstDrive.Items.Add(di);
        }
        private void lstFolders_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstFiles.Items.Clear();
            DirectoryInfo dir = (DirectoryInfo)lstFolders.SelectedItem;
            foreach (FileInfo fi in dir.GetFiles())
                lstFiles.Items.Add(fi);
        }
        private void lstDrive_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstFolders.Items.Clear();
            try
            {
                DriveInfo drive = (DriveInfo)lstDrive.SelectedItem;
                foreach (DirectoryInfo dirInfo in drive.RootDirectory.GetDirectories())
                    lstFolders.Items.Add(dirInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            //I don't know If I need this?
        }
        }
   }

从ListBox中选择并显示要在PictureBox中显示的图像文件?C#

尝试如下。。。它会起作用。。。。

private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
  pictureBox1.Image = Image.FromFile(((FileInfo)lstFiles.SelectedItem).FullName);
}