问题描述
我想用我的相机捕捉到一个网络摄像头饲料。对于我使用的是2个基准:AForge.Video.dll和AForge.Video.DirectShow.dll
。我看着下面的基本途径,以获得饲料描述这里一>
要在这里总结一下上面的链接是在code ...
公共FilterInfoCollection CamsCollection;
公共VideoCaptureDevice凸轮= NULL;
无效Cam_NewFrame(对象发件人,EventArgs的NewFrameEventArgs)
{
frameholder.Source =(位图)eventArgs.Frame.Clone(); / *这里躺着的错误,因为它可以隐式地从System.Drawing.Bitmap到System.Windows.Media.ImageSource不转换* /
}
私人无效startcam_Click(对象发件人,RoutedEventArgs E)
{
CamsCollection =新FilterInfoCollection(FilterCategory.VideoInputDevice);
凸轮=新VideoCaptureDevice(CamsCollection [1] .MonikerString);
Cam.NewFrame + =新NewFrameEventHandler(Cam_NewFrame);
Cam.Start();
}
私人无效stopcam_Click(对象发件人,RoutedEventArgs E)
{
Cam.Stop();
}
以上方式使用图片框来显示帧。正如我感兴趣的WPF中,我用了给出下面的方式here
要在这里介绍了就是我的code貌似现在。
公共FilterInfoCollection CamsCollection;
公共VideoCaptureDevice凸轮= NULL;
无效Cam_NewFrame(对象发件人,EventArgs的NewFrameEventArgs)
{
为System.Drawing.Image imgforms =(位图)eventArgs.Frame.Clone();
BitmapImage的BI =新的BitmapImage();
bi.BeginInit();
MemoryStream的毫秒=新的MemoryStream();
imgforms.Save(MS,ImageFormat.Bmp);
ms.Seek(0,SeekOrigin.Begin);
bi.StreamSource =毫秒;
frameholder.Source =双向; / *在这里获得运行时错误。双向是否已被其他线程占用。* /
bi.EndInit();
}
私人无效startcam_Click(对象发件人,RoutedEventArgs E)
{
CamsCollection =新FilterInfoCollection(FilterCategory.VideoInputDevice);
凸轮=新VideoCaptureDevice(CamsCollection [1] .MonikerString);
Cam.NewFrame + =新NewFrameEventHandler(Cam_NewFrame);
Cam.Start();
}
私人无效stopcam_Click(对象发件人,RoutedEventArgs E)
{
Cam.Stop();
}
的问题:
-
我知道上面code已经得到了一些重大的错误,我得到的运行时错误:变量'双向'被另一个线程。 (附注:我新来编程)
-
有人可以提供一个更好的方法,而不是使用memorystreams ......这整个的方式似乎有点哈克。
感谢
-Sagar
EDIT1:作为一个详细的解释查看我的博文关于同一主题。
我想我找到了解决问题的办法。它是我希望具有并由此得到错误的UI元素的只是异步运行。我纠正使用调度错误如下:
无效Cam_NewFrame(对象发件人,EventArgs的NewFrameEventArgs)
{
为System.Drawing.Image imgforms =(位图)eventArgs.Frame.Clone();
BitmapImage的BI =新的BitmapImage();
bi.BeginInit();
MemoryStream的毫秒=新的MemoryStream();
imgforms.Save(MS,ImageFormat.Bmp);
ms.Seek(0,SeekOrigin.Begin);
bi.StreamSource =毫秒;
bi.EndInit();
//使用冻结功能,以避免交叉线程操作
bi.Freeze();
//使用分派更新图像WPF控件调用UI线程
Dispatcher.BeginInvoke(新的ThreadStart(委托
{
frameholder.Source =双向; / * frameholder是图像WPF控件的名称* /
}));
}
现在它运行正常,我也得到没有太多的框架滞后不错的表现。 :)
I want to capture a webcam feed using my camera. For that I am using the 2 references: AForge.Video.dll and AForge.Video.DirectShow.dll.
I looked at the following basic way to get the feed described here
To sum up the above link here's the code...
public FilterInfoCollection CamsCollection;
public VideoCaptureDevice Cam = null;
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
frameholder.Source = (Bitmap)eventArgs.Frame.Clone(); /* Here lies the error, as it cannot convert implicitly from System.Drawing.Bitmap to System.Windows.Media.ImageSource*/
}
private void startcam_Click(object sender, RoutedEventArgs e)
{
CamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
Cam = new VideoCaptureDevice(CamsCollection[1].MonikerString);
Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
Cam.Start();
}
private void stopcam_Click(object sender, RoutedEventArgs e)
{
Cam.Stop();
}
The above way uses a picturebox to display the frames. As I am interested in WPF, I used thefollowing way given here
To brief up here's what my code looks like NOW.
public FilterInfoCollection CamsCollection;
public VideoCaptureDevice Cam = null;
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();
BitmapImage bi=new BitmapImage();
bi.BeginInit ();
MemoryStream ms= new MemoryStream ();
imgforms.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
frameholder.Source = bi; /*getting runtime error here. bi is occupied by some other thread.*/
bi.EndInit();
}
private void startcam_Click(object sender, RoutedEventArgs e)
{
CamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
Cam = new VideoCaptureDevice(CamsCollection[1].MonikerString);
Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
Cam.Start();
}
private void stopcam_Click(object sender, RoutedEventArgs e)
{
Cam.Stop();
}
The problems:
I know the above code has got some major errors as I get the runtime error: the variable 'bi' is used by another thread. (p.s. i m new to programming)
Can someone suggest a better way rather than using memorystreams... This entire way seems to be a bit hacky.
Thanks
-Sagar
Edit1: for a detailed explanation view my blogpost on the same topic.
I think I found the solution to the problem.It was just the asynchronous running of the UI elements that I desired to have and was hence getting the error. I corrected the error using the dispatcher as follows:
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
imgforms.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
//Using the freeze function to avoid cross thread operations
bi.Freeze();
//Calling the UI thread using the Dispatcher to update the 'Image' WPF control
Dispatcher.BeginInvoke(new ThreadStart(delegate
{
frameholder.Source = bi; /*frameholder is the name of the 'Image' WPF control*/
}));
}
Now it runs as expected and I get good performance without much frame lag. :)
这篇关于帮助需要获得在C#和WPF使用Aforge.NET网络摄像头流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!