I have a webcam capture app and I'm trying to implement a video preview in my WPF UI. Capturing happens in a C++ DLL. I have a current solution but I'm not too happy with it. The main goal is that the video preview in the UI doesn't interfere with the C++ DLL much, as it has to compress the video frames and write them to disk. I won't be able to display every frame, because the compression is fairly CPU-intensive.
My current video preview solution is:
An Image control is used in my window, and its Source attribute is data-binded to a BitmapSource called VideoPreviewSource
I set up a System.Threading.TimerCallback to update my preview. The timer callback function dispatches to a thread which can update the UI (using this.Dispatcher.BeginInvoke) which calls UpdatePreview()
UpdatePreview() asks the C++ DLL for a video frame. The call to the DLL puts the image's raw data in byte[] _rawImageData, which I allocate once in my window's constructor.
UpdatePreview() then sets VideoPreviewSource to: BitmapSource.Create(width, height, 96, 96, pf, null, _rawImageData, width * 4); Finally OnPropertyChanged("VideoPreviewSource") is called.
It seems like if I make the timer more frequent, garbage collection is called less often and it basically looks like a memory leak.
Any better approach to a video preview?