Methods of drawing video feed in WPF
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.开发者_Python百科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
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.
精彩评论