函數原型
[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);
參數
          hDestDC:指向目標設備環境的 
         句柄。 
        
 
        
          x:指定目標矩形區域左上角的X軸邏輯坐標。 
        
 
        
          y:指定目標矩形區域左上角的Y軸邏輯坐標。 
        
 
        
          nWidth:指定源在目標矩形區域的邏輯寬度。 
        
 
        
          nHeight:指定源在目標矩形區域的邏輯高度。 
        
 
        
          hSrcDC:指向源設備環境的句柄。 
        
 
        
          xSrc:指定源矩形區域左上角的X軸邏輯坐標。 
        
 
        
          ySrc:指定源矩形區域左上角的Y軸邏輯坐標。 
        
 
         
         
        
          dwRop:指定光柵操作代碼。這些代碼將定義源矩形區域的顏色數據,如何與目標矩形區域的顏色數據組合以完成最后的顏色。 
        
 
        
          下面列出了一些常見的 
         光柵操作代碼: 
        
 
        
          BLACKNESS:表示使用與物理 
         調色板的索引0相關的色彩來填充目標矩形區域,(對缺省的物理調色板而言,該顏色為黑色)。 
        
 
        
          DSTINVERT:表示使目標矩形區域顏色取反。 
        
 
        
          MERGECOPY:表示使用布爾型的AND(與)操作符將源矩形區域的顏色與特定模式組合一起。 
        
 
        
          MERGEPAINT:通過使用布爾型的OR(或)操作符將反向的源矩形區域的顏色與目標矩形區域的顏色合並。 
        
 
         
         
        
          NOTSRCCOPY:將源矩形區域顏色取反,於拷貝到目標矩形區域。 
        
 
        
          NOTSRCERASE:使用布爾類型的OR(或)操作符組合源和目標矩形區域的顏色值,然后將合成的顏色取反。 
        
 
        
          PATCOPY:將特定的模式拷貝到目標位圖上。 
        
 
        
          PATPAINT:通過使用布爾OR(或)操作符將源矩形區域取反后的顏色值與特定模式的顏色合並。然后使用OR(或)操作符將該操作的結果與目標矩形區域內的顏色合並。 
        
 
        
          PATINVERT:通過使用XOR( 
         異或)操作符將源和目標矩形區域內的顏色合並。 
        
 
        
          SRCAND:通過使用AND(與)操作符來將源和目標矩形區域內的顏色合並。 
        
 
        
          SRCCOPY:將源矩形區域直接拷貝到目標矩形區域。 
        
 
         
         
        
          SRCERASE:通過使用AND(與)操作符將目標矩形區域顏色取反后與源矩形區域的顏色值合並。 
        
 
        
          SRCINVERT:通過使用布爾型的XOR(異或)操作符將源和目標矩形區域的顏色合並。 
        
 
        
          SRCPAINT:通過使用布爾型的OR(或)操作符將源和目標矩形區域的顏色合並。 
        
 
         
        
          dwRop Values 
        
 
        
          From wingdi.h: 
        
 
        
          #define BLACKNESS 0x42 
        
 
        
          #define DSTINVERT 0x550009 
        
 
        
          #define MERGECOPY 0xC000CA 
        
 
        
          #define MERGEPAINT 0xBB0226 
        
 
        
          #define NOTSRCCOPY 0x330008 
        
 
        
          #define NOTSRCERASE 0x1100A6 
        
 
        
          #define PATCOPY 0xF00021 
        
 
        
          #define PATINVERT 0x5A0049 
        
 
        
          #define PATPAINT 0xFB0A09 
        
 
        
          #define SRCAND 0x8800C6 
        
 
        
          #define SRCCOPY 0xCC0020 
        
 
         
         
        
          #define SRCERASE 0x440328 
        
 
        
          #define SRCINVERT 0x660046 
        
 
        
          #define SRCPAINT 0xEE0086 
        
 
        
          #define WHITENESS 0xFF0062 
        
 
        返回值
          如果函數成功,那么返回值非零;如果函數失敗,則返回值為零。 
        
 
        
          Windows NT:若想獲取更多錯誤信息,請調用GetLastError函數。 
        
 
        
          備注:如果在源設備環境中可以實行旋轉或 
         剪切變換,那么函數BitBlt返回一個錯誤。如果存在其他變換(並且目標設備環境中匹配變換無效),那么目標設備環境中的矩形區域將在需要時進行拉伸、壓縮或旋轉。 
        
 
        
          如果源和目標設備環境的顏色格式不匹配,那么BitBlt函數將源場景的顏色格式轉換成能與目標格式匹配的格式。當正在記錄一個增強型 
         圖元文件時,如果源設備環境標識為一個增強型圖元文件設備環境,那么會出現錯誤。如果源和目標設備環境代表不同的設備,那么BitBlt函數返回錯誤。 
        
 
        
          Windows CE:在Windows CE 1.0版中,參數dwRop只可以指定為下列值:SRCCOPY、SRCAND、SRCPAINT、SRCINVERT。在Windows CE 2.0版中,參數dwRop可以是任何光柵操作代碼值。 
        
 
        
          速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;頭文件:wingdi.h;庫文件:gdi32.lib。 
        
 
        按句柄截圖 、直接截取縮略圖
 
           
          public static class ImageHelper { public static Bitmap CaptureWindow(IntPtr handle, int width, int height) { try { // get the hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return img; } catch (Exception ex) { LogHelper.Execption(ex, nameof(ImageHelper)); } return null; } public static Bitmap CaptureWindow(IntPtr handle, int widthSrc, int heightSrc, int widthDest, int heightDest) { try { // get the hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, widthDest, heightDest); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.SetStretchBltMode(hdcDest, GDI32.STRETCH_HALFTONE); GDI32.POINTAPI point; GDI32.SetBrushOrgEx(hdcDest, 0, 0, out point); // bitblt over GDI32.StretchBlt(hdcDest, 0, 0, widthDest, heightDest, hdcSrc, 0, 0, widthSrc, heightSrc, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return img; } catch (Exception ex) { LogHelper.Execption(ex, nameof(ImageHelper)); } return null; } /// <summary> /// Helper class containing Gdi32 API functions /// </summary> public class GDI32 { public const int CAPTUREBLT = 1073741824; public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter [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); /// <summary> /// 縮放截圖 /// </summary> /// <param name="hdcDest"></param> /// <param name="nXOriginDest"></param> /// <param name="nYOriginDest"></param> /// <param name="nWidthDest"></param> /// <param name="nHeightDest"></param> /// <param name="hdcSrc"></param> /// <param name="nXOriginSrc"></param> /// <param name="nYOriginSrc"></param> /// <param name="nWidthSrc"></param> /// <param name="nHeightSrc"></param> /// <param name="dwRop"></param> /// <returns></returns> [DllImport("gdi32.dll")] public static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, int dwRop); public const int STRETCH_ANDSCANS = 0x01; public const int STRETCH_ORSCANS = 0x02; public const int STRETCH_DELETESCANS = 0x03; public const int STRETCH_HALFTONE = 0x04; /// <summary> /// 設置縮放模式 /// </summary> /// <param name="hdc"></param> /// <param name="iStretchMode"></param> /// <returns>失敗返回0</returns> [DllImport("gdi32.dll")] public static extern int SetStretchBltMode(IntPtr hdc, int iStretchMode); [StructLayout(LayoutKind.Sequential)] public struct POINTAPI { public int x; public int y; } [DllImport("gdi32.dll")] public static extern bool SetBrushOrgEx(IntPtr hdc, int nXOrg, int nYOrg, out POINTAPI lppt); } /// <summary> /// Helper class containing User32 API functions /// </summary> public 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 ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("user32.dll")] public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); public const int WM_PAINT = 0x000F; [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern uint SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] public static extern bool PrintWindow( IntPtr hwnd, // Window to copy,Handle to the window that will be copied. IntPtr hdcBlt, // HDC to print into,Handle to the device context. UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values. ); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); #region 窗口關聯 // nCmdShow的含義 //0 關閉窗口 //1 正常大小顯示窗口 //2 最小化窗口 //3 最大化窗口 //使用實例: ShowWindow(myPtr, 0); #endregion } }
          延展閱讀:  
         MSDN Bitmap Functions 
        
 
        