Reputation: 366
I have a very basic problem. I have a variable of type: Bitmap. This variable updates with every frame received from the webcam. This variable is declared as follows:
Bitmap img=eventArgs.Frame;
I have verified that the above line is working properly. All I need to know now is how to set my PictureBox on my form to this Bitmap image. I have tried the following:
pbImg.Image=img;
This doesn't work. Any help would be greatly appreciated.
Upvotes: 1
Views: 955
Reputation: 941465
Bitmaps generated by a camera normally only have a very short life-time. They are only valid while the event handler runs, the camera capture driver replaces the bitmap with a new frame. Pretty essential to avoid excessive memory usage.
You must therefore make a deep copy of the image so that it can survive in the PictureBox and still get painted after the event call completed. Like this:
Bitmap img = new Bitmap(eventArgs.Frame);
if (pbImg.Image != null) pbImg.Image.Dispose();
pbImg.Image = img;
Upvotes: 3