我正在尝试使listBox( lstFiles )可选,因此它可以在pictureBox( pictureBox1 )中显示图像文件,并在从中选择另一个文件后更改listBox,我创建了一个网络摄像头程序,可以拍摄有效但无法显示图像的图片。
我尝试了很多方法,但是从列表框中选择文件名
无法使其工作任何帮助都将不胜感激,谢谢
这是我到目前为止所做的:
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?
}
}
}
答案 0 :(得分:1)
尝试如下......它会起作用....
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)lstFiles.SelectedItem).FullName);
}