【发布时间】:2013-12-06 10:04:08
【问题描述】:
我想在 Aforge.NET 的 c# 代码中使用网络摄像头,但它不起作用,因为我不使用表单。
请参阅下面的代码:
public partial class CapturePage : Page
{
private bool DeviceExist = false;
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource = null;
public CapturePage()
{
InitializeComponent();
getCamList();
startVideo();
}
// get the devices name
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
}
catch (ApplicationException)
{
DeviceExist = false;
}
}
//toggle start and stop button
private void startVideo() // as it's say
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); // the only one webcam
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
CloseVideoSource();
// videoSource.DesiredFrameSize = new Size(160, 120); // deprecated ?
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
}
else
{
// error
}
}
else
{
if (videoSource.IsRunning)
{
timer1.Enabled = false;
CloseVideoSource();
}
}
}
//eventhandler if new frame is ready
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
//do processing here
pictureBox1.Image = img; // But I can't have pictureBox in xaml ??
}
//close the device safely
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
}
}
我在表单应用程序中尝试了这段代码,它可以工作,但是在页面中,没有引用 PictureBox。即使我引用它并在代码中创建一个,这也不起作用。
我可以以及如何在 XAML 页面中使用 PictureBox 吗?或者在 Xaml 页面中使用 Aforge.net 网络摄像头还有其他解决方案吗?
【问题讨论】:
-
您查看过他们的文档吗?他们的网站?您是否尝试使用任何 WPF 图像控件或只是复制代码?
-
我尝试使用图像,结果相同:没有。我检查了他们的文档,但没有找到有用的东西。
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。