示例#1
0
        public static Bitmap PrtWindow(IntPtr hWnd)
        {
            IntPtr    hscrdc = User32Api.GetWindowDC(hWnd);
            Rectangle rect   = Rectangle.Empty;

            User32Api.GetWindowRect(hWnd, ref rect);
            IntPtr hbitmap = Gdi32Help.CreateCompatibleBitmap(hscrdc, rect.Width - rect.Left, rect.Height - rect.Top);
            IntPtr hmemdc  = Gdi32Help.CreateCompatibleDC(hscrdc);

            Gdi32Help.SelectObject(hmemdc, hbitmap);
            User32Api.PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);

            Gdi32Help.DeleteDC(hscrdc);
            Gdi32Help.DeleteDC(hmemdc);
            return(bmp);
        }
示例#2
0
        /// <summary>
        /// 截屏1
        /// </summary>
        /// <param name="windowModel"></param>
        /// <returns></returns>
        public static Bitmap PrintWindow(WindowModel windowModel)
        {
            IntPtr hscrdc  = User32Api.GetWindowDC(windowModel.IntPtr); //返回hWnd参数所指定的窗口的设备环境。
            int    width   = windowModel.WindowRectangle.Width;
            int    height  = windowModel.WindowRectangle.Height;
            IntPtr hbitmap = Gdi32Help.CreateCompatibleBitmap(hscrdc, width, height); //该函数创建与指定的设备环境相关的设备兼容的位图
            IntPtr hmemdc  = Gdi32Help.CreateCompatibleDC(hscrdc);                    //该函数创建一个与指定设备兼容的内存设备上下文环境(DC)

            Gdi32Help.SelectObject(hmemdc, hbitmap);                                  //该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象
            User32Api.PrintWindow(windowModel.IntPtr, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);

            //Clipboard.SetImage(bmp);
            Gdi32Help.DeleteDC(hscrdc); //删除用过的对象  
            Gdi32Help.DeleteDC(hmemdc); //删除用过的对象  
            return(bmp);
        }