所有人。我一直被困在這裏處理這些錯誤的日子,但我仍然無法弄清楚。 我的猜想:我認爲我的代碼有一些問題,因爲我在使用它之後沒有正確地處理對象(我不太瞭解釋放資源,線程的這些概念)。 我通過參考人們在YouTube上做過的事情得到了這些代碼,但儘管我做了完全相同的事情,但我的代碼並沒有很好地工作。Aforge.net相機捕捉並保存圖像到目錄
狀態: 我有兩個圖片框,左側可以拍攝我的視頻,右側拍攝快照,如果按下按鈕1,您將開始播放視頻,clone_button將複製圖像,即拍攝快照,save_image應該將它保存到路徑引用中,但是,當我試圖保存它時,我一次又一次地在GDI +中發生了一個通用錯誤。另外,一旦我運行這個程序時,我的調試器似乎變得瘋狂(即無法終止vshost.exe),我必須重新啓動計算機才能讓代碼再次運行,這是令人沮喪和沮喪的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
//AForge.Video dll
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge;
namespace WebCameraCapture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection CaptureDevice; // list of webcam
private VideoCaptureDevice FinalFrame;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);//constructor
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0; // default
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);// specified web cam and its filter moniker string
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);// click button event is fired,
FinalFrame.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) // must be void so that it can be accessed everywhere.
// New Frame Event Args is an constructor of a class
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();// clone the bitmap
}
private void From1_CLosing(object sender, EventArgs e)
{
if (FinalFrame.IsRunning==true) FinalFrame.Stop();
}
private void save_Click(object sender, EventArgs e)
{
if (pictureBox2.Image != null)
{
Bitmap varBmp = new Bitmap(pictureBox2.Image);
Bitmap newBitmap = new Bitmap(varBmp);
varBmp.Dispose();
varBmp = null;
varBmp.Save(@"C:\a.png", ImageFormat.Png);
}
else
{ MessageBox.Show("null exception"); }
}
private void clone_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
}
}
任何AForge.net用戶都可以按下下面的鏈接並試用它。謝謝!
超級,工作真棒..... – Sunil