Performance update - Better image framerate in WPF
I am wo开发者_开发知识库rking in WPF in .Net 4.0. I have some huge images from camera 1392x1040 pixels. Every frame comes as System.Drawing.Bitmap
and will be converted into a BitmapImage by using
Public Function BitmapToWpfBitmapSource(ByVal bmSrc As System.Drawing.Bitmap) As BitmapSource
If bmSrc Is Nothing Then Return Nothing
Dim res As BitmapSource
Dim Ptr As IntPtr
Ptr = bmSrc.GetHbitmap(System.Drawing.Color.Black) 'Create GDI bitmap object
res = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Ptr, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmSrc.Width, bmSrc.Height))
Dim ret As Integer = DeleteObject(Ptr) 'Delete GDI bitmap object
GC.Collect() 'Because the code is not managed, we need to call the collector manually to avoid memory spikes
Return res
End Function
If I update the images in GUI I can get about 7 frames / second.
There were some possibilities to increase speed by decreasing quality:
- Render with Nearest neighbor
RenderOptions.SetBitmapScalingMode(Me.ucWindow1.VideoPresenter1.img1, BitmapScalingMode.NearestNeighbor)
Work with threads to update every frame
Dim dl As New SetImageDelegate(AddressOf UpdateImageInGuiGuiThread) Me.Dispatcher.Invoke(dl, imgSrc)
Tested with 32Bit and 24Bit images - compare Imageformat
Using Performance profiling suite for framerate count
But CPU is still about 10% and not 100% and framerate is about 12FPS maximum instead of 39 (Winforms).
How to improve framerates from camera?
I suspect your GPU load is high. When I have done similar programs, the WPF rendering uses a lot of GPU resources and this can easily be a bottleneck.
If CPU is 10% then something besides the processing of your image is happening. I suspect the answer to your question is something that you have not mentioned here. eg reading from disk or reading from camera.
精彩评论