C# Bitmap LockBits/UnlockBits in multi-thread

前端 未结 1 1524
走了就别回头了
走了就别回头了 2021-01-28 13:54

I am working on a CCTV project which employs ONVIF. I use a Winform sample, which is provided by \"ONVIF Device Manager\" project, to obtain video frames from a camera. (You can

相关标签:
1条回答
  • 2021-01-28 14:51

    You get the error "Bitmap is already locked" because of the following line:

    Bitmap bmp = img as Bitmap;
    

    It seems that img is declared globally, and it is being used both by your thread, and the UI thread ath the same time. When a Bitmap object is being displayed in UI, it is being Locked by UI thread for painting. The Lock method in your thread conflicts with this operation in UI thread.

    To get better performance, I recommend you to generate a Bitmap for each frame you get in your thread. Then BeginInvoke it to display the prepared image. In UI thread you should care for disposing the Bitmap when replacing in PictureBox property.

    0 讨论(0)
提交回复
热议问题