我在 C# windows 窗体中制作了一个程序我的窗体中有大量功能,包括两个连接到 dabase 的数据网格视图,包括一个直接连接到我的 PC 的相机我使用 AForge dll 引用连接到相机设备我刚刚找到了教程在youtube上,它对我来说非常有效,正如我之前所说,我在一种形式中有太多的程序,包括那个相机,结果相机需要调整到一个小分辨率,所以我决定制作一个弹出按钮当我单击表单上的按钮时,必须显示更宽的分辨率。
这是我相机的代码。
//Camera
// get the devices name
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture device on your system");
}
}
//refresh button
private void refresh_Click(object sender, EventArgs e)
{
getCamList();
}
//toggle start and stop button
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
lblCam.Text = "Device running...";
start.Text = "&Stop";
}
else
{
lblCam.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
CloseVideoSource();
lblCam.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
//eventhandler if new frame is ready
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
//do processing here
pictureBox1.Image = img;
}
//close the device safely
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
//prevent sudden close while device is running
private void Form1_FormClosed(object sender, FormClosingEventArgs e)
{
CloseVideoSource();
}
} }
我还发布了一张图片,以便您进一步了解我在说什么。 正如您在右下角看到的那样,我有一个弹出按钮,老实说,我已经尝试过不同的方法,但没有任何效果,不幸的是我无法发布我尝试过的内容,因为我昨天创建了它并且无法再撤消我尝试过的代码. 任何想法?