1

我正在尝试使我的列表框(lstFiles)可选择,因此它能够在图片框(图片框1)中显示图像文件并在从列表框中选择另一个文件后进行更改,我创建了一个可以拍摄照片的网络摄像头程序,但遇到问题显示图像。

我尝试了很多方法,但无法通过从列表框中选择文件名来使其工作

任何帮助将不胜感激谢谢

这是我到目前为止所拥有的:

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?
        }

        }
   }
4

1 回答 1

1

尝试如下...它会工作....

private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
  pictureBox1.Image = Image.FromFile(((FileInfo)lstFiles.SelectedItem).FullName);
}
于 2013-02-09T11:39:16.247 回答