内存不足以捕获屏幕截图

时间:2013-12-30 21:06:04

标签: c# winforms screenshot

您好,我有问题。我有一个设置来捕获我的WebBrowser控件的屏幕截图:

public static class Utilities
{
    public const int SRCCOPY = 13369376;

    public static Image CaptureScreen()
    {
        return CaptureWindow(User32.GetDesktopWindow());
    }

    public static Image CaptureWindow(IntPtr handle)
    {

        IntPtr hdcSrc = User32.GetWindowDC(handle);

        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(handle, ref windowRect);

        int width = windowRect.right - windowRect.left;
        int height = windowRect.bottom - windowRect.top;

        IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

        IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
        Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
        Gdi32.SelectObject(hdcDest, hOld);
        Gdi32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);

        Image image = Image.FromHbitmap(hBitmap);
        Gdi32.DeleteObject(hBitmap);

        return image;
    }
}

public static class ControlExtensions
{
    public static Image DrawToImage(this Control control)
    {
        return Utilities.CaptureWindow(control.Handle);
    }
}

public class Gdi32
{
    [DllImport("gdi32.dll")]
    public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
    [DllImport("gdi32.dll")]
    public static extern bool DeleteDC(IntPtr hDC);
    [DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);
    [DllImport("gdi32.dll")]
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}

public static class User32
{
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll")]
    public static extern IntPtr GetDesktopWindow();
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
    [DllImport("user32.dll")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
}

我有一个100毫秒的计时器,以10fps的速度在一个图片框中显示WebBrowser:

    private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBox2.Image = ControlExtensions.DrawToImage(webBrowser1);
    }

但是从这个方面来说,我的记忆每分钟增加100mb。

我是否遗漏了清除记忆的东西?

// EDIT @ N4TKD现在我明白了:

        if (pictureBox2.Image != null)
            pictureBox2.Image.Dispose();
        pictureBox2.Image = ControlExtensions.DrawToImage(webBrowser1);

谢谢

2 个答案:

答案 0 :(得分:6)

垃圾收集器将在您之后清理,但它无法正确完成Image类的工作。这是一个非常小的托管包装类,围绕着一大堆非托管内存,用于存储图像的像素数据。在创建足够的Image对象以触发GC之前,您将耗尽内存。

显式处理旧图像必需

答案 1 :(得分:0)

我的GDI在操作顺序上有点生疏......但您是否尝试在Gdi.DeleteDC(hdcDest)之后移动Gdi32.DeleteObject(hBitmap)

看起来DeleteDC应该是用该DC清理所有相关对象后做的最后一件事。

以下是我想重新排序的方法:

    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);