i've try the following way to record the video from the Webcam by 25 Frame-rate per sec for 10sec but when i get the out put video it is of 2sec and the frames are played to fast as compare to the video stream.
The code is as follows.
using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Threading;
using AForge.Video.FFMPEG;
namespace AforgeTutorial
{
public partial class Form1 : Form
{
private FilterInfoCollection ListOfCams;
private VideoCaptureDevice SelectedCam; //From where we will take image
System.Timers.Timer tim;
Thread t;
bool isNewFrame = false;
VideoFileWriter writer;
public Form1()
{
InitializeComponent();
tim = new System.Timers.Timer(10000);
tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed);
t = new Thread(saveVideo);
}
void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (isRecord)
{
writer.Close();
isRecord = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
ListOfCams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (ListOfCams.Count == 0)
return;
comboBox1.Items.Clear();
foreach (FilterInfo Cam in ListOfCams)
{
comboBox1.Items.Add(Cam.Name);
}
}
private void StopCamera()
{
SelectedCam.SignalToStop();
SelectedCam.Stop();
}
bool isRecord = false;
private void Start_Click(object sender, EventArgs e)
{
if (comboBox1.Text == string.Empty)
return;
SelectedCam = new VideoCaptureDevice(ListOfCams[comboBox1.SelectedIndex].MonikerString);
SelectedCam.NewFrame += new NewFrameEventHandler(SelectedCam_NewFrame);
SelectedCam.Start();
}
Bitmap image;
void SelectedCam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
image = (Bitmap)eventArgs.Frame.Clone();
isNewFrame = true;
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StopCamera();
}
private void Stop_Click(object sender, EventArgs e)
{
StopCamera();
}
private void btnRecord_Click(object sender, EventArgs e)
{
tim.Start();
if (!isRecord)
{
writer = new VideoFileWriter();
writer.Open(#"C:/code-bude_test_video.mp4", 640, 480, 25, VideoCodec.MPEG4,10000);
}
isRecord = !isRecord;
if (isRecord)
t.Start();
}
void saveVideo()
{
while (isRecord)
{
if (isNewFrame)
{
writer.WriteVideoFrame(image);
isNewFrame = false;
}
}
}
}
}
You have multi-threading issues in your code. You can't write to shared variables like that and expect it to synchronize.
You have three threads: user interface, streaming and saving. (SelectedCam_NewFrame runs in the AForge streaming thread). Make a list of all variables that are accessed in at least two threads (isRecord, isNewFrame, etc) and add proper synchronization.
You can check this very good reference on threading in C#.
Note that even with synchronization, you may miss frames if your writer thread is busy while several images arrive. What you might want to do is collect the frames produced by the camera in a queue and consume that queue in the writer thread. Check the producer/consumer patterns.
Related
I need to update a DataGridView that has master detail part, I'm trying to use this control for the grid (I use 4 datasets for filling the grid).
Question: how can I add correctly a thread or more that will add to the grid these datasets and show the correct percentage and the same time the UI remain usable(responsive, not to become "not responding" for user)?
In every table I have about 10000 rows.
Please provide some code if possible, because I'm stuck.
Thanks in advance,
Steve
I tried like this, but I'm stuck at showing the user the correct percentage and correctly using the thread:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MasterDetail
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
MasterControl masterDetail;
private void frmMain_Load(object sender, EventArgs e)
{
//label1.Text = DateTime.Now.ToString();
clearFields();
loadData();
//label2.Text = DateTime.Now.ToString();
}
void clearFields()
{
panelView.Controls.Clear();
masterDetail = null;
Refresh();
masterDetail = new MasterControl(nwindDataSet);
panelView.Controls.Add(masterDetail);
}
internal delegate void SetDataSourceDelegate();
private void setDataSource()
{
if (this.masterDetail.InvokeRequired)
{
this.masterDetail.BeginInvoke(new SetDataSourceDelegate(setDataSource));
}
else
{
createMasterDetailView();
}
}
void loadData()
{
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(delegate() { loadDataThread(); }));
thread.Start();
}
void loadDataThread()
{
orderReportsTableAdapter.Fill(nwindDataSet.OrderReports);
invoicesTableAdapter.Fill(nwindDataSet.Invoices);
employeesTableAdapter.Fill(nwindDataSet.Employees);
customersTableAdapter.Fill(nwindDataSet.Customers);
setDataSource();
}
void createMasterDetailView()
{
masterDetail.setParentSource(nwindDataSet.Customers.TableName, "CustomerID");
masterDetail.childView.Add(nwindDataSet.OrderReports.TableName, "Orders");
masterDetail.childView.Add(nwindDataSet.Invoices.TableName, "Invoices");
masterDetail.childView.Add(nwindDataSet.Employees.TableName, "Employees");
typeof(DataGridView).InvokeMember("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty, null,
masterDetail, new object[] { true });
foreach (DataGridView dvg in masterDetail.childView.childGrid)
{
typeof(DataGridView).InvokeMember("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty, null,
dvg, new object[] { true });
}
masterDetail.childView.childGrid[2].RowTemplate.Height = 100;
}
private void btnLoad_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
}
}
I think it would be better for you to use a backgroundworker
It has an event called WorkerReportsProgress that can help you create the progressbar you need.
Here is the sample code from msdn:
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void startAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void cancelAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
backgroundWorker1.CancelAsync();
}
}
// This event handler is where the time-consuming work is done.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
// This event handler updates the progress.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
}
// This event handler deals with the results of the background operation.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
resultLabel.Text = "Canceled!";
}
else if (e.Error != null)
{
resultLabel.Text = "Error: " + e.Error.Message;
}
else
{
resultLabel.Text = "Done!";
}
}
you just need to add the proper computation of the percentage and you're done
The code that I got was from naudio record sound from microphone then save and many thanks to Corey
This is the error message that I get when I run the code for the second or subsequent times.
The first time it runs, it runs with no issues what so ever.
If I change the file name it works perfectly.
Unable to copy file "obj\Debug\Basque.exe" to "bin\Debug\Basque.exe". The process cannot access the file 'bin\Debug\Basque.exe' because it is being used by another process. Basque
Could someone gave me some guidance to where I'm making my error
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;
namespace Basque
{
public partial class FlashCard : Form
{
public WaveIn waveSource = null;
public WaveFileWriter waveFile = null;
public FlashCard()
{
InitializeComponent();
StopBtn.Enabled = false;
StartBtn.Enabled = true;
}
private void StartBtn_Click(object sender, EventArgs e)
{
StartBtn.Enabled = false;
StopBtn.Enabled = true;
waveSource = new WaveIn();
waveSource.WaveFormat = new WaveFormat(44100, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
waveFile = new WaveFileWriter(#"C:\Temp\bas0001.wav", waveSource.WaveFormat);
waveSource.StartRecording();
}
private void StopBtn_Click(object sender, EventArgs e)
{
StopBtn.Enabled = false;
waveSource.StopRecording();
}
void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveFile != null)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
waveFile.Flush();
}
}
void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
if (waveFile != null)
{
waveFile.Dispose();
waveFile = null;
}
StartBtn.Enabled = true;
}
private void PlayBtn_Click(object sender, EventArgs e)
{
}
private void ExitBtn_Click(object sender, EventArgs e)
{
}
}
}
It might help to put a Formclosing method with Application.Exit(); in it. If it only works on the first try, it might be because the application isn't fully closing.
You can check if this will fix it when you check task manager. Just make sure that your application isn't still there. Even if it isn't still there, the Application.Exit(); might help.
I am developing a Desktop Application which requires me to connect to webcam(s) and record(save) the video in MPEG, AVI, MP4 and WMV formats and Burn into the CD/DVD. The application is in Win Forms. I am only Looking for free or open source solutions or controls.
I had done saving as AVI using Aforge.Net but its taking more size to save(like 60-100MB for 15sce 320x240 Video ). I am expecting 1MB for 10sec.
Here is the Code :
using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.VFW;
namespace Aforge_Web_Cam
{
public partial class VideoForm : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo = null;
private VideoCaptureDeviceForm captureDevice;
private Bitmap video;
private AVIWriter AVIwriter = new AVIWriter();
private SaveFileDialog saveAvi;
public VideoForm()
{
InitializeComponent();
}
private void VideoForm_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
captureDevice = new VideoCaptureDeviceForm();
}
private void butStart_Click(object sender, EventArgs e)
{
if (captureDevice.ShowDialog(this) == DialogResult.OK)
{
VideoCaptureDevice videoSource = captureDevice.VideoDevice;
FinalVideo = captureDevice.VideoDevice;
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (butStop.Text == "Stop Record")
{
video = (Bitmap)eventArgs.Frame.Clone();
pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
AVIwriter.Quality = 0;
AVIwriter.AddFrame(video);
}
else
{
video = (Bitmap)eventArgs.Frame.Clone();
pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
}
}
private void butRecord_Click(object sender, EventArgs e)
{
saveAvi = new SaveFileDialog();
saveAvi.Filter = "Avi Files (*.avi)|*.avi";
if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
AVIwriter.Open(saveAvi.FileName, w, h);
butStop.Text = "Stop Record";
//FinalVideo = captureDevice.VideoDevice;
//FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
//FinalVideo.Start();
}
}
private void butStop_Click(object sender, EventArgs e)
{
if (butStop.Text == "Stop Record")
{
butStop.Text = "Stop";
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
//this.FinalVideo.Stop();
this.AVIwriter.Close();
pbVideo.Image = null;
}
}
else
{
this.FinalVideo.Stop();
this.AVIwriter.Close();
pbVideo.Image = null;
}
}
private void butCapture_Click(object sender, EventArgs e)
{
pbVideo.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
}
private void butCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void VideoForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
this.FinalVideo.Stop();
this.AVIwriter.Close();
}
}
}
}
AVIWriter doesn't provide videocompression, use FileWriter from AForge.Video.FFMPEG. There you can choose everything: Size, Framerate, Codec and Bitrate and if your video was 600MB in 20 sec it now will be 6 MB in 20 sec.
Here you go:
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 AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Video.VFW;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo = null;
private VideoCaptureDeviceForm captureDevice;
private Bitmap video;
//private AVIWriter AVIwriter = new AVIWriter();
private VideoFileWriter FileWriter = new VideoFileWriter();
private SaveFileDialog saveAvi;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
captureDevice = new VideoCaptureDeviceForm();
}
private void button1_Click(object sender, EventArgs e)
{
if (captureDevice.ShowDialog(this) == DialogResult.OK)
{
VideoCaptureDevice videoSource = captureDevice.VideoDevice;
FinalVideo = captureDevice.VideoDevice;
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (butStop.Text == "Stop Record")
{
video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
//AVIwriter.Quality = 0;
FileWriter.WriteVideoFrame(video);
//AVIwriter.AddFrame(video);
}
else
{
video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
}
private void button2_Click(object sender, EventArgs e)
{
saveAvi = new SaveFileDialog();
saveAvi.Filter = "Avi Files (*.avi)|*.avi";
if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
FileWriter.Open(saveAvi.FileName, w, h,25,VideoCodec.Default,5000000);
FileWriter.WriteVideoFrame(video);
//AVIwriter.Open(saveAvi.FileName, w, h);
butStop.Text = "Stop Record";
//FinalVideo = captureDevice.VideoDevice;
//FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
//FinalVideo.Start();
}
}
private void butStop_Click(object sender, EventArgs e)
{
if (butStop.Text == "Stop Record")
{
butStop.Text = "Stop";
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
//this.FinalVideo.Stop();
FileWriter.Close();
//this.AVIwriter.Close();
pictureBox1.Image = null;
}
}
else
{
this.FinalVideo.Stop();
FileWriter.Close();
//this.AVIwriter.Close();
pictureBox1.Image = null;
}
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
this.FinalVideo.Stop();
FileWriter.Close();
//this.AVIwriter.Close();
}
}
}
}
I've been trying to get the BackGroundWorker to work for hours now. I can't seem to find out why my DoWork() eventhandler isn't getting raised after calling bgw.RunWorkerAsync();
I've cut down the irrelivant code, so it's a bit easyer to read.
namespace FolderMonitor
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using FolderMonitor.Properties;
public partial class MainForm : Form
{
private ExistingFileHandler exist = new ExistingFileHandler();
private MonitoredFileHandler handler = new MonitoredFileHandler();
private Monitor monitor;
private BackgroundWorker bgw = new BackgroundWorker();
public MainForm(Monitor monitor)
{
this.monitor = monitor;
InitializeComponent();
InitializeBackgroundWorker();
txtFolderPath.Text = Settings.Default.monitoredFolder;
txtNewFolderPath.Text = Settings.Default.destinationFolder;
btnStop.Enabled = false;
}
private void InitializeBackgroundWorker()
{
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
}
private void BtnStart_Click(object sender, EventArgs e)
{
btnStop.Enabled = true;
btnStart.Enabled = false;
exist.HandleExistingFiles(txtFolderPath.Text);
listBoxFiles.Items.Clear();
MonitoredFileHandler.MonitoredFolderPath = txtFolderPath.Text;
MonitoredFileHandler.DestinationFolderPath = txtNewFolderPath.Text;
this.bgw.RunWorkerAsync();
}
private void BtnStop_Click(object sender, EventArgs e)
{
this.bgw.CancelAsync();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (!worker.IsBusy)
{
monitor.StartFolderMonitor(txtFolderPath.Text);
}
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnStart.Enabled = true;
btnStop.Enabled = false;
}
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
handler.MonitoredFiles.Add(txtNewFolderPath.Text);
}
}
}
BackgroundWorker.IsBusy will be true after its background thread been started, so it will always be true where you are testing it inside bgw_DoWork(). Therefore, monitor.StartFolderMonitor(txtFolderPath.Text) will never be called.
What are you trying to test for? I think you can just remove the if (!worker.IsBusy) check.
Aside the aforementioned IsBusy issue, the fact that StartFolderMonitor begins with start as opposed to DoFolderMonitoring or something similar, it is perhaps non-blocking therefore the DoWork handler exits immediately.
Just call
System.Windows.Forms.Application.DoEvents();
before bgw.RunWorkAsync();
I am trying to retrieve images from media library to a warp panel inside listbox 'lstImageFromMediaLibrary', also am trying that while the images load i show a loading screen using a usercontrol and adding it to popup.child
but i am getting this exceeption 'UnauthorizedAccessException'
when i remove all backgrougWorker related code no such unauthorized access is there....
void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
{
foreach (Picture p in mediaLibrary.Pictures)
{
bitmapImage.SetSource(p.GetThumbnail());
lstBitmapImage.Add(bitmapImage);
}
this.lstImageFromMediaLibrary.ItemsSource = lstBitmapImage;
}
any help is appriciated , i hope i made myself clear....
EDIT:
ok so now m doing this
BackgroundWorker backroungWorker = new BackgroundWorker();
Popup popup = new Popup();
public PanoramaPage1()
{
InitializeComponent();
showpopup();
init();
}
private void init()
{
backroungWorker.WorkerReportsProgress = true;
backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork);
backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);
backroungWorker.ProgressChanged+=new ProgressChangedEventHandler(backroungWorker_ProgressChanged);
backroungWorker.RunWorkerAsync();
}
void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
popup.IsOpen = false;
}
);
}
void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
{
backroungWorker.ReportProgress(10);
}
void backroungWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.InitializePage();
}
private void showpopup()
{
popup.Child = new SplashScreenControl();
popup.Width = 480;
popup.IsOpen = true;
}
private void InitializePage()
{
MediaLibrary mediaLibrary = new MediaLibrary();
List<BitmapImage> lstBitmapImage = new List<BitmapImage>();
foreach (Picture p in mediaLibrary.Pictures)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(p.GetThumbnail());
lstBitmapImage.Add(bitmapImage);
}
this.lstImageFromMediaLibrary.ItemsSource = lstBitmapImage;
}
but still the progress bar just shows a dot and nothing else.....
You are accessing your User Interface in your DoWork Event. You should be communicating to your application through the Background Worker Events such as the ProgressChanged or the RunWorkerCompleted Events.
From First link:
You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the BackgroundWorker events.