【发布时间】:2019-03-07 13:13:51
【问题描述】:
我正在从事一个采用 ONVIF 的闭路电视项目。我使用“ONVIF 设备管理器”项目提供的 Winform 示例从相机获取视频帧。 (你可以找到它here)。我发现该示例使用 dispatcher.BeginInvoke() 在 UI 线程中放置了 CopyMemory() 块代码。我会减慢主 UI 线程,因为重复此块以在 PictureBox 中显示图像。
void InitPlayback(VideoBuffer videoBuffer, bool isInitial)
{
//....
var renderingTask = Task.Factory.StartNew(delegate
{
var statistics = PlaybackStatistics.Start(Restart, isInitial);
using (videoBuffer.Lock())
{
try
{
//start rendering loop
while (!cancellationToken.IsCancellationRequested)
{
using (var processingEvent = new ManualResetEventSlim(false))
{
var dispOp = disp.BeginInvoke((MethodInvoker)delegate
{
using (Disposable.Create(() => processingEvent.Set()))
{
if (!cancellationToken.IsCancellationRequested)
{
//update statisitc info
statistics.Update(videoBuffer);
//render farme to screen
//DrawFrame(bitmap, videoBuffer, statistics);
DrawFrame(videoBuffer, statistics);
}
}
});
processingEvent.Wait(cancellationToken);
}
cancellationToken.WaitHandle.WaitOne(renderinterval);
}
}
catch (OperationCanceledException error) { } catch (Exception error) { } finally { }
}
}, cancellationToken);
}
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, int count);
private void DrawFrame(VideoBuffer videoBuffer, PlaybackStatistics statistics)
{
Bitmap bmp = img as Bitmap;
BitmapData bd = null;
try
{
bd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//bgra32
using (var md = videoBuffer.Lock())
{
CopyMemory(bd.Scan0, md.value.scan0Ptr, videoBuff.stride * videoBuff.height);
//bitmap.WritePixels(
// new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
// md.value.scan0Ptr, videoBuffer.size, videoBuffer.stride,
// 0, 0
//);
}
}
catch (Exception err)
{
//errBox.Text = err.Message;
Debug.Print("DrawFrame:: " + err.Message);
}
finally
{
bmp.UnlockBits(bd);
}
imageBox.Image = bmp;
// var dispOp = disp.BeginInvoke((MethodInvoker)delegate {imageBox.Image = bmp;}); =>>Bitmap is already locked
}
我试图通过在 UnlockBits() 位图之后调用 BeginInvoke() 来排除 UI 线程之外的 CopyMemory() 语句。但是,会引发错误“位图已锁定”。有 one question 已发布,我已按照该问题的答案进行操作,但在重绘 imageBox 时出现另一个错误“参数无效”。我想如果我们锁定位图 lock(bmp) {CopyMemory();...} imageBox 无法获取与其关联的位图信息。
非常感谢任何帮助。
更新建议的解决方案
private void DrawFrame(PlaybackStatistics statistics)
{
Bitmap bmp = new Bitmap(videoBuff.width, videoBuff.height);//img as Bitmap;
//...
imageBox.Invoke((MethodInvoker)delegate
{
Image bmTemp = imageBox.Image;
imageBox.Image = bmp;
if (bmTemp != null)
{
bmTemp.Dispose();
}
});
}
【问题讨论】:
-
我可能会为此使用BackgroundWorker,因为您只是从 UI 线程卸载图像处理。
-
您还应该将视频的读取和表单上的显示分开,而不是将 bmp 分配给 imageBox 的使用,引发一个刷新事件,该事件将由您的 imageBox OnPaint 事件接管绘制当前图像。
-
@LaurentLequenne,引发刷新事件将导致此issue。
-
我猜你的bmp可以通过从视频中获取新图像来锁定......所以你必须每次创建一个新图像,并将结果克隆到将重绘的全局图像在 onpaint 事件中。
-
顺便说一句...从不做
imageBox.Image.Dispose()。您需要将imageBox.Image的内容存储在一个单独的变量中,然后将imageBox.Image的内容更改为其他内容,然后然后从单独的变量。否则,在某个时刻,活动的 UI 元素将有一个已处置的对象链接到它,并且当它尝试重新绘制 UI 时,您将得到一个ObjectDisposedException。
标签: c# multithreading bitmap onvif lockbits