性能更新 - WPF中更好的图像帧速率

Nas*_*aer 5 .net wpf performance image image-processing

我在.Net 4.0中的WPF工作.我有一些相机1392x1040像素的巨大图像.每个帧都会出现,System.Drawing.Bitmap并将通过使用转换为BitmapImage

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
Run Code Online (Sandbox Code Playgroud)

如果我在GUI中更新图像,我可以获得大约7帧/秒.

图像帧率计数

通过降低质量可以提高速度:

  • 与最近的邻居一起渲染

RenderOptions.SetBitmapScalingMode(Me.ucWindow1.VideoPresenter1.img1, BitmapScalingMode.NearestNeighbor)

  • 使用线程更新每一帧

    Dim dl As New SetImageDelegate(AddressOf UpdateImageInGuiGuiThread)Me.Dispatcher.Invoke(dl,imgSrc)

  • 使用32位和24位图像进行测试 - 比较Imageformat

使用性能分析套件进行帧率计数

但CPU仍然是大约10%而不是100%,帧率大约是12FPS而不是39(Winforms).

如何提高相机的帧率?

Mik*_*lls 1

如果 CPU 为 10%,那么除了图像处理之外还会发生其他事情。我怀疑你的问题的答案是你在这里没有提到的。例如从磁盘读取或从相机读取。