<分区>
我在让 AForge.NET 与我的 Windows 窗体应用程序一起运行时遇到了很多麻烦。该应用程序需要一个位图流来在自定义 PictureBox 中显示视频,同时使用相同的流来跟踪使用颜色的对象。
我尝试锁定 NewFrame 事件,将图像从视频源复制到临时图像,如下所示,使用监视器。
//Event for when a frame from the video is ready
videoSource.NewFrame += (s, e) =>
{
if (System.Threading.Monitor.TryEnter(updaterLock, 20))
{
Bitmap old = currentImage;
currentImage = (Bitmap)e.Frame.Clone();
currentImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
if (currentImage != null)
{
if (ImageUpdated != null)
ImageUpdated(this, EventArgs.Empty);
if (old != null)
{
old.Dispose();
old = null;
}
}
else
currentImage = old;
System.Threading.Monitor.Exit(updaterLock);
}
};
上面的代码是返回单例实例的类的一部分,该实例通过属性访问 currentImage。可以找到完整的类 here . 在自定义控件中,像这样访问位图(DisplayControl 类持有对 RgbStream 实例的引用 - 位图流):
Control.DisplayControl.Instance.ImageUpdated += (s, e) => this.Image = Control.DisplayControl.Instance.Bitmap;
访问控件(this.Image)的Image属性时抛出异常(InvalidOperationException),如下所示:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Cross-thread operation not valid: Control 'gridControl' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.set_Size(Size value)
at SystemInterface.GUI.Controls.OccupancyGridControl.set_Image(Image value) in c:\Users\Stefan\SW505\root\ProductCode\GUI\Controls\OccupancyGridControl.cs:line 64
at SystemInterface.GUI.Controls.OccupancyGridControl.<.ctor>b__0(Object s, EventArgs e) in c:\Users\Stefan\SW505\root\ProductCode\GUI\Controls\OccupancyGridControl.cs:line 207
at Control.DisplayControl.<.ctor>b__0(Object s, EventArgs e) in c:\Users\Stefan\SW505\root\ProductCode\Control\DisplayControl.cs:line 36
at System.EventHandler.Invoke(Object sender, EventArgs e)
at Services.CameraServices.RgbStream.<.ctor>b__0(Object s, NewFrameEventArgs e) in c:\Users\Stefan\SW505\root\ProductCode\Services\CameraServices\RgbStream.cs:line 121
at AForge.Video.DirectShow.VideoCaptureDevice.OnNewFrame(Bitmap image)
at AForge.Video.DirectShow.VideoCaptureDevice.Grabber.BufferCB(Double sampleTime, IntPtr buffer, Int32 bufferLen)
InnerException:
关于如何解决这个问题有什么想法吗?谢谢:)