'USB Cam Feed Not Displaying In Picturebox Using C# And Aforge
I have a C# Winforms project in which I have to capture an image from an external webcam (Logitech HD Pro C920). I am using the Aforge media library. The list of video input devices are showing up, and the laptop's internal webcam is connecting and the stream is being displayed in the picturebox.
However the stream from the USB cam is not being displayed in the picturebox even though it is listed as a video input device. The relevant code is shown below:
cam = new VideoCaptureDevice(webcam[cbCameras.SelectedIndex].MonikerString);
cam.NewFrame +=new NewFrameEventHandler(cam_NewFrame);
cam.Start();
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
picPhoto.Image = bit;
}
Where am I missing it? Thanks
Solution 1:[1]
Finally I found the solution by changing the way I used to select the webcam. Now, I opted for the form provided by AForge (VideoCaptureDeviceForm).
Here is the Code:
private void BtnCamSelection_Click(object sender, RoutedEventArgs e)
{
VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();
if(form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.pVideoSource = form.VideoDevice;
}
}
Then, I loaded the aforge:VideoSourcePlayer
with the variable pVideoSource
.
Solution 2:[2]
Try this:
public VideoCapabilities[] videoCapabilities;
videoCapabilities = Cam.VideoCapabilities;
Cam.VideoResolution = videoCapabilities[0];
Solution 3:[3]
From the question asked on here before How initialize AForge webcam
public Form1() // init
{
InitializeComponent();
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = video;
}
private void button2_Click(object sender, EventArgs e)
{
FinalVideo.Stop();
}
}
Solution 4:[4]
I also had the same problem as shown here. There is a fix and a 'Workaround'.
Using the VideoCaptureDeviceForm does work and this is fine if you want to display this form.
The Problem
It appears the Logitect C920 doesn't have a default video resolution, or AForge isn't setting it. Other webcams seem to perform correctly.
The Fix
I was able to fixed the issue going on Laurenz Albes answer. This will just set the default resolution to the first in the array of camera supported resolutions, which seems to be the lowest. You can add logic to set the resoution you want.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Emma |
Solution 2 | Laurenz Albe |
Solution 3 | Jess Chan |
Solution 4 | Chris Birch |