在WPF中绘制视频源的方法
首先,我想向社区问好。我一直是这个地方的潜伏者和欣赏者。
现在,我正在开发一个个人运动检测程序,该程序包含 Aforge 库,并且强烈基于位于此处的 Andrew Kirillov 的教程,http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx?fid=177045&fr=26#xx0xx。顺便说一句,我强烈推荐给任何对该主题感兴趣的人。现在进入正题。该项目是使用 winforms 构建的,我正在使用 WPF 重新创建我自己的版本(位、片段和附加内容),但我遇到了问题。有一节Andrew使用了OnPaint函数,我正在努力解决如何在wpf中处理绘图。这是他的代码(可能有细微的修改)
// Paint control
protected override void OnPaint(PaintEventArgs pe)
{
// lock
Monitor.Enter(this);
var g = pe.Graphics;
var rc = ClientRectangle;
var pen = new Pen(m_RectColor, 1);
// draw rectangle
g.DrawRectangle(pen, rc.X, rc.Y, rc.Width - 1, rc.Height - 1);
if (m_Camera != null)
{
try
{
m_Camera.Lock();
// draw frame
g.DrawImage(m_Camera.LastFrame, rc.X + 1, rc.Y + 1, rc.Width - 2, rc.Height - 2);
}
catch (Exception) { }
finally
{
m_Camera.Unlock();
}
}
pen.Dispose();
// unlock
Monitor.Exit(this);
base.OnPaint(pe);
}
为了提供更多的见解,m_Camera.LastFrame 是一个位图。
经过一番研究后,我发现有一个 onrender 函数可用,但由于效率问题,似乎没有人很关心它。我还考虑了分配建立图像并将位图分配为源的可能性,但这对我来说并不可行。任何建议将不胜感激。另外,如果需要更多信息/代码,请告诉我。谢谢!
丹尼尔
First off, I want to say hello to the community. I have been a long lurker and appreciator of this place.
Now to the point, I am working on a personal motion detection program that is incorporating Aforge libraries and is strongly based off Andrew Kirillov's tutorial located here, http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx?fid=177045&fr=26#xx0xx. Which by the way, I would HIGHLY recommend to anyone that has interest in the topic. Now to the point. This project is built using winforms, I am recreating my own version (bits, pieces, and additions) using WPF and I have run into an issue. There is a section in which Andrew makes use of the OnPaint function, and I am struggling on how to handle the drawing in wpf. Here is his code (possible with minor alterations)
// Paint control
protected override void OnPaint(PaintEventArgs pe)
{
// lock
Monitor.Enter(this);
var g = pe.Graphics;
var rc = ClientRectangle;
var pen = new Pen(m_RectColor, 1);
// draw rectangle
g.DrawRectangle(pen, rc.X, rc.Y, rc.Width - 1, rc.Height - 1);
if (m_Camera != null)
{
try
{
m_Camera.Lock();
// draw frame
g.DrawImage(m_Camera.LastFrame, rc.X + 1, rc.Y + 1, rc.Width - 2, rc.Height - 2);
}
catch (Exception) { }
finally
{
m_Camera.Unlock();
}
}
pen.Dispose();
// unlock
Monitor.Exit(this);
base.OnPaint(pe);
}
To provide a little more insight, m_Camera.LastFrame is a bitmap.
After doing some research I found that there is an onrender function available, but no one seems to care for it much due to efficiency. I also considered the possibility of assigned establishing an image and assigning the bitmap as the source, but that is not working out for me. Any advice would be appreciated. Also if more information/code is needed, please let me know. Thanks!
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 DrawingVisual 中使用 WPF 渲染上下文以与 Winforms 所示的完全相同的方式进行绘制。主要区别在于,在 WPF 中,它仍然不是立即模式绘制。所有渲染上下文绘制都像所有其他 WPF 操作一样被记录并按计划渲染。就我的目的而言,它工作得很好,但我不确定您是否会看到良好的视频性能。
正如您提到的,您也可以分配位图。我不确定为什么这对你不起作用。也许您需要一些 UpdateLayout 调用来强制显示更新?
另一种方法是使用 WriteableBitmap,但同样,这不是立即模式绘制,因为 WPF 中没有立即模式绘制。不过,我确实从中获得了相当不错的性能,包括动画。
最后,您可以将 WPF 用于除视频表面之外的所有内容,并使用 Winforms 主机在其中托管视频面板。大多数 WPF 功能都可以在 Winforms 主机上正常工作,主要的例外是 Flyover。
You can use the WPF rendering context in a DrawingVisual to draw in the exact manner that you have shown for Winforms. The major difference is that in WPF it still isn't immediate mode drawing. All render context drawing is recorded just as all other WPF operations are and rendered on a schedule. For my purposes it has worked fine but I am not sure you'd see good performance for video.
You can assign a bitmap as well, as you mention. I am not sure why that isn't working for you. Perhaps you need some UpdateLayout calls to force the update to show?
Another method is to use a WriteableBitmap, but again, that isn't immediate mode drawing as there is no immediate mode drawing in WPF. I do get pretty good performance out of it though, including animations.
Finally you can use WPF for everything except your video surface and use the Winforms host to host your video panel within. Most WPF features work just fine with the Winforms host, the main exception being flyover.