C# 使用user32.dll根据窗口句柄获取窗口截图

文章描述了一段C#代码在获取窗口截图时出现内存泄漏的bug,由于未正确释放DC和Bitmap对象的句柄导致内存持续增长。通过引入`ReleaseDC`和`DeleteObject`函数来释放资源,解决了内存泄漏的问题,测试后内存不再持续上升。
摘要由CSDN通过智能技术生成

百度到的代码今天发现有个bug,代码链接为C# 根据窗口句柄获取窗口截图,代码如下

public class WindowCaptureHelper
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
        [DllImport("gdi32.dll")]
        private static extern int DeleteDC(IntPtr hdc);
        [DllImport("user32.dll")]
        private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, int nFlags);
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);

        public static Bitmap GetShotCutImage(IntPtr hWnd)
        {
            var hscrdc = GetWindowDC(hWnd);
            var windowRect = new Rectangle();
            GetWindowRect(hWnd, ref windowRect);
            int width = Math.Abs(windowRect.Width- windowRect.X);
            int height = Math.Abs(windowRect.Height- windowRect.Y);
            var hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
            var hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, hbitmap);
            PrintWindow(hWnd, hmemdc, 0);
            var bmp = Image.FromHbitmap(hbitmap);
            DeleteDC(hscrdc);
            DeleteDC(hmemdc);
            return bmp;
        }
    }

客户说一天后就崩了,看了一下任务管理器,程序运行内存没有变化.但是过了一个小时,任务管理器总内存涨了10%,大概800M,这应该就是使用dll未释放指针了.使用ChatGPT,把代码输入进去,直接为我找到了问题.

    [DllImport("user32.dll")]
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    [DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);

    public static Bitmap GetShotCutImage(IntPtr hWnd)
    {
        var hscrdc = GetWindowDC(hWnd);
        var windowRect = new Rectangle();
        GetWindowRect(hWnd, ref windowRect);
        int width = Math.Abs(windowRect.Width - windowRect.X);
        int height = Math.Abs(windowRect.Height - windowRect.Y);
        var hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
        var hmemdc = CreateCompatibleDC(hscrdc);
        SelectObject(hmemdc, hbitmap);
        PrintWindow(hWnd, hmemdc, 0);
        var bmp = Image.FromHbitmap(hbitmap);
        DeleteObject(hbitmap);
        ReleaseDC(hWnd, hscrdc);
        //DeleteDC(hscrdc); //原代码,造成了内存未释放
        DeleteDC(hmemdc);
        return bmp;
    }

测试了20分钟,内存没有上升了

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值