c# picturebox memory releasing problem

前端 未结 3 1179
别那么骄傲
别那么骄傲 2020-12-17 17:50

I\'m a newby in C#. I have to repeatedly refresh a GUI picture box in a worker thread. The image is acquired from a camera polling a driver with a GetImage method that retri

3条回答
  •  再見小時候
    2020-12-17 18:22

    Image implements IDisposable, so you should call Dispose on each Image instance that you create, when it is no longer needed. You could try to replace this line in your code:

    pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
    

    With this:

    if (pic.Image != null)
    {
        pic.Image.Dispose();
    }
    pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
    

    This will dispose the previous image (if any) before the new one is assigned.

提交回复
热议问题