BitBlt function details, StretchBlt, SetStretchBltMode, SetBrushOrgEx screenshots by handle, directly captures thumbnails, bitbltstretchblt

Source: Internet
Author: User

BitBlt function details, StretchBlt, SetStretchBltMode, SetBrushOrgEx by handle, directly cut the thumbnail, bitbltstretchblt
BitBlt this function performs bit_block conversion on pixels in the specified source device environment area to transmit the data to the target device environment. Function prototype

            [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: The handle pointing to the target device environment. X: Specifies the x-axis logical coordinates in the upper left corner of the target rectangle. Y: Specifies the y-axis logical coordinate in the upper left corner of the target rectangle. NWidth: Specifies the logical width of the source in the target rectangle area. NHeight: Specifies the logical height of the source in the target rectangular area. HSrcDC: the handle to the Source Device environment. XSrc: Specifies the x-axis logical coordinates in the upper left corner of the source rectangle area. YSrc: Specifies the y-axis logical coordinates in the upper left corner of the source rectangle area. Dwdrop: Specify the grating operation code. The Code defines the color data of the source rectangle area and how to combine the color data of the target rectangle area to complete the final color. The following lists some common raster operation codes: BLACKNESS: Fill the target rectangular area with colors related to index 0 of the physical palette (for the default physical palette, the color is black ). DSTINVERT: indicates that the color of the target rectangle is reversed. MERGECOPY: Use the AND (AND) operator of the boolean type to combine the color of the source rectangle area with the specific pattern. MERGEPAINT: uses the Boolean OR (OR) operator to combine the color of the Reverse source rectangle area with the color of the target rectangle area. NOTSRCCOPY: returns the color of the source rectangle and copies it to the target rectangle. NOTSRCERASE: Use the Boolean OR (OR) operator to combine the color values of the source and target rectangle areas, and then reverse the merged color. PATCOPY: copy a specific pattern to the target map. PATPAINT: Use the Boolean OR (OR) operator to combine the color value after the inverse of the source rectangle area with the color of the specific pattern. Then, use the OR (OR) operator to merge the result of this operation with the color in the area of the target rectangle. PATINVERT: uses the XOR (exclusive or) operator to merge colors in the source and target Rectangular areas. SRCAND: uses the AND (AND) operator to merge colors in the source AND target rectangle areas. SRCCOPY: copy the source rectangle area directly to the target rectangle area. SRCERASE: Use the AND (AND) operator to reverse the color of the target rectangle area AND combine it with the color value of the source rectangle area. SRCINVERT: uses the Boolean XOR (exclusive or) operator to merge the colors of the source and target Rectangular areas. SRCPAINT: merges the colors of the source and target Rectangular areas by using the Boolean OR (OR) operator. WHITENESS: Fill the target rectangular area with colors related to index 1 in the physical palette. (This color is white for the default physical palette ). DwRop ValuesFrom wingdi. h: # define BLACKNESS 0x42 # define DSTINVERT 0x550009 # define MERGECOPY finished # define MERGEPAINT finished # define NOTSRCCOPY 0x330008 # define NOTSRCERASE 0x1100A6 # define PATCOPY finished # define PATINVERT finished # PATPAINT 0xFB0A09 # define SRCAND 0x8800C6 # define SRCCOPY 0xCC0020 # define SRCERASE 0x440328 # define SRCINVERT 0x660046 # define SRCPAINT 0xEE0086 # define WHITENESS 0xF The return value of F0062 is non-zero if the function is successful. If the function fails, the return value is zero. Windows NT: To obtain more error information, call the GetLastError function. NOTE: If rotation or shear transformation can be performed in the source device environment, BitBlt returns an error. If there are other transformations (and the matching transformations in the target device environment are invalid), the rectangular area in the target device environment will be stretched, compressed, or rotated as needed. If the color format of the source and target device environments does not match, the BitBlt function converts the color format of the source scenario to a format that matches the target format. When an enhanced primitive file is being recorded, an error occurs if the source device environment is identified as an enhanced primitive file device environment. If the source and target device environments represent different devices, the BitBlt function returns an error. Windows CE: in Windows CE 1.0, the dwdrop parameter can only be specified as the following values: SRCCOPY, SRCAND, SRCPAINT, and SRCINVERT. In Windows CE 2.0, the dwRop parameter can be any grating operation code value. Quick query: Windows NT: 3.1 and later; Windows: 95 and later; Windows CE: 1.0 and later; header file: wingdi. h; library file: gdi32.lib. Extract the thumbnail public static class ImageHelper {public static Bitmap CaptureWindow (IntPtr handle, int width, int height) by handle) {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, 0, GDI32.SRCCOPY); // restore selection failed (hdcDest, hOld); // clean up GDI32.DeleteDC (hdcDest); User32.ReleaseDC (handle, hdcSrc); // get. 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 = values (hdcDest, hBitmap); values (hdcDest, GDI32.STRETCH _ HALFTONE); GDI32.POINTAPI point; values (hdcDest, 0, 0, out point ); // bitblt over partition (hdcDest, 0, 0, widthDest, heightDest, hdcSrc, 0, 0, widthSrc, heightSrc, GDI32.SRCCOPY); // restore selection partition (hdcDest, hOld ); // clean up GDI32.DeleteDC (hdcDest); User32.ReleaseDC (handle, hdcSrc); // get. 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 ("failed")] public static extern bool BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwdrop); [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> /// zoom /// </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 values (IntPtr hdcDest, int values, int nYOriginDest, int values, int values, 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> /// set the scaling mode /// </summary> /// <param name = "hdc"> </param> /// <param name = "iStretchMode"> </param> // <returns> 0 is returned for failure </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 funyout // </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 get1_topwindow (); [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 Window Association // nCmdShow definition // 0 Close Window // 1 normal size display window // 2 minimize window // 3 maximize window // use instance: ShowWindow (myPtr, 0); # endregion }}Screenshot thumbnails by handle Demo

 

 

StretchBltSetStretchBltModeSetBrushOrgEx reference material: Baidu Baike additional description: MSDN Bitmap Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.