Out of Memory from capuring screenshots

后端 未结 2 1304
挽巷
挽巷 2021-01-15 05:21

Hello there I have a problem. I have a setup to capture Screenshots of my WebBrowser control:

public static class Utilities
{
    public const int SRCCOPY =          


        
相关标签:
2条回答
  • 2021-01-15 05:44

    My GDI is a bit rusty on order of operations... but have you tried moving Gdi.DeleteDC(hdcDest) after Gdi32.DeleteObject(hBitmap)?

    It would seem like DeleteDC should be the last thing you do after cleaning up all associated objects with that DC.

    Here is how I would think to reorder it:

        IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
        Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
    
        Image image = Image.FromHbitmap(hBitmap);
    
        Gdi32.SelectObject(hdcDest, hOld);
        Gdi32.DeleteObject(hBitmap);
    
        Gdi32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);
    
    0 讨论(0)
  • 2021-01-15 06:07

    The garbage collector will clean up after you, but it can't get the job done properly for the Image class. Which is a very small managed wrapper class around a big pile of unmanaged memory that stores the pixel data of an image. You will run out of memory before you've created enough Image objects to trigger a GC.

    Explicitly disposing the old images is required.

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