在 Silverlight 中,我试图在一系列 3d 四边形上获取网络摄像头(实时)流的帧。我在网络摄像头控制器类的网络摄像头控制器类中使用 VideoSink。然后我在 DrawingSurface 中绘制四边形。但我一直在 CrossAppDomainMarshaledException 中运行。作为一种解决方案,我尝试使用 Dispatcher.BeginInvoke 但有时 BeginInvoke 中的代码似乎跳过或跳出线程。如何处理这样的事情?
//Video sink capture
// Is called every time the webcam provides a complete frame (Push)
protected override void OnSample(long sampleTime, long frameDuration, byte[] sampleData)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
WriteableBitmap bmp = new WriteableBitmap(vidFormat.PixelWidth, vidFormat.PixelHeight);
RaiseFrameCapture(new FrameCapturedEventArgs { Frame = bmp.FromByteArray(sampleData) });
});
}
//Capture from sink into WebCamController
void sink_FrameCaptured(object sender, FrameCapturedEventArgs e)
{
//List<WriteableBitmap>
_WebCamSource.AddImage(e.Frame.Clone());
}
//XNA draw event handler
private void DrawingSurface_Draw(object sender, DrawEventArgs e)
{
List<WriteableBitmap> frames = new List<WriteableBitmap>();
if (webCamSource.Frames.Count > 0)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
frames = new List<WriteableBitmap>(webCamSource.Frames.ToArray());
});
}
Draw(frames);
e.InvalidateSurface();
}