你好,我有一个问题.我有一个设置来捕获我的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.
我错过了清除记忆的东西吗?
//编辑@ N4TKD现在我明白了:
if (pictureBox2.Image != null) pictureBox2.Image.Dispose(); pictureBox2.Image = ControlExtensions.DrawToImage(webBrowser1);
谢谢
垃圾收集器将在您之后清理,但它无法正确完成Image类的工作.这是一个非常小的托管包装类,围绕着一大堆非托管内存,用于存储图像的像素数据.在创建足够的Image对象以触发GC之前,将耗尽内存.
明确处置旧影像时需要.