How to capture image with webcam? - c#

I want to capture image with webcam.
I use WebCam_Capture.dll. i add a WebCamCapture control in form.
when form load:
private void Form1_Load(object sender, EventArgs e)
{
this.WebCamCapture.TimeToCapture_milliseconds = 1;
WebCamCapture.Start(0);
}
and in ImageCaptured event :
private void WebCamCapture_ImageCaptured(object source,
WebCam_Capture.WebcamEventArgs e)
{
this.pictureBox1.Image = e.WebCamImage;
}
but when i run, i get error:
An error ocurred while capturing the video image. The video capture will now be terminated.
Object reference not set to an instance of an object.

I recommend you to use OpenCV Library.
it is an open source library for image proccessing from intel, developed by intel.
but, it has a wrapper for .net, you can download this wrapper from : this link

Related

How to paste clipboard data to another application in c# windows form

I have a form where i have copied a text in clipboard, now my requirement is that after setting clipboard data if i click in inside another application like notepad
data gets pasted there.
private void ListView1_EPC_Click(object sender, EventArgs e)
{
string str = ListView1_EPC.SelectedItems[0].SubItems[1].Text;
Clipboard.SetText(str);
}
thanks in advance.
That is possible: You can attach to the system wide mousedown. This has already been implemented in C#, have a look here... https://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
This project provides a simple event for a global mouse down (GlobalEventProvider component). What you then need to do is to send a ctrl-v using SendKeys, when this mouse down has been triggered...
public class ForeignWindowPasteHelper
{
private readonly GlobalEventProvider eventProvider = new GlobalEventProvider();
public ForeignWindowPasteHelper()
{
this.eventProvider.MouseClick += this.GlobalMouseDown;
}
private void GlobalMouseDown(object sender, MouseEventArgs e)
{
SendKeys.Send("^{v}");
}
}
This will trigger a Cntrl+V (Insert) on every keydown. You will need to implement some logic, when this needs to be triggered. But thats not hard.

EmguCV not reading camera

I have the the following code in a normal windows form application with EmguCV 3.1
public Form1()
{
InitializeComponent();
_capture = new Capture("http://root:pass#192.168.1.27:80/axis-cgi/mjpg/video.cgi");
_capture.ImageGrabbed += ProcessFrame;
}
private void Form1_Load(object sender, EventArgs e)
{
_capture.Start();
}
private void ProcessFrame(object sender, EventArgs e)
{
Mat image = new Mat();
_capture.Retrieve(image);
imageBox1.BackgroundImage = image.Bitmap;
}
I have tested the above link in a browser it worked, I have also tested this using iSpy it also works there but using EmguCV ProcessFrame is never reached
I have also tried to connect to the camera using Luxand and it worked well but Luxand is not free so I have to use EmguCV to do face detection & recognition
Looking at this post, try adding ?x.mjpeg after .cgi in your url.

Cant view webcam after being ran once. C#

the title may sound confusing but ill explain it better here. Im making a program which displays the webcam capture in a picturebox using the "easy web cam" external reference. If i turn on my computer, go into VS, open the project and run, it will work, displaying my webcam capture. If i stop the program and then run it again, when i try to display it, i get a popup asking me to select a video source, none of the options is even my webcam and then another popup will appear saying
"An error ocurred while capturing the video image. The video capture will now be terminated.
Object reference not set to ann instance of an object"
The only thing i can think of is that the first time its setting up the camera but when i close it im not turning it off properly so when i run it again, it wont work. anyway heres the relevant code, bare in mind if answering, im not that experienced when coding so sometimes you might have to spell stuff out
using WebCam_Capture;
namespace WindowsWebRef
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
WebCam webcam;
private void button1_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void Frm_Main_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref WebCamIMG);
}
And the webcam class...
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
Restart your computer and be sure to add in webcam.Stop(); otherwise your program will hold on to the webcam and make it unavailable to use in any other application (or a different instance of the program.)

Previewing images in a listBox using pictureBox

I am using Winforms to create a 2D Map Editor.
I want to be able to preview an image of my assets that are stored in listBox using a pictureBox.
My current code for doing so is thus.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)listBox_Assets.SelectedItem).FullName);
}
But when I select an asset I get this error.
Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
I have searched high and low for a solution but can't find an answer to this error, any help would be greatly appreciated.
You use the file name from the listbox like this, and protect the code with a check for the file.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
string file = IO.Path.Combine("the directory", listBox_Assets.SelectedItem);
if (IO.File.Exists(file))
pictureBox1.Image = Image.FromFile(file);
}

how to reload video in axVLCPlugin21 using C#

How to reload video in axVLCPlugin21 using C# my code just working one time and just open one video although open another video this my code:
private void panel2Open_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
axVLCPlugin21.playlist.add(openFileDialog1.FileName);
axVLCPlugin21.playlist.play();
axVLCPlugin21.playlist.items.clear();
}
}
It is working fine for me even I used it by getting video links for Datagridview:
string videoSource=dataGridView1.Rows[e.RowIndex].Cells["Link"].Value.ToString();
axVLCPlugin21.playlist.add(videoSource, "video", " ");
axVLCPlugin21.playlist.next();
axVLCPlugin21.playlist.play();

Categories