C # region screenshot & mdash; call API screenshot,

Source: Internet
Author: User

C # region -- call API,

For a C ++ developer, it is nothing more than a piece of cake. Some friends also use C # Graphics. copyFromScreen method screen operations, as a beginner C # Who wants to develop a game with C ++ all day long #. NET programmers, I am only studying the area by myself, failed n times, and finally made a simple Demo in the middle of the dream, just to share it. Such a shift is really bad. In addition, My C ++ level is at the entry level. If you have any questions about this blog, please leave a message and correct it. Thank you for your attention!

Attachment and others:

Https://msdn.microsoft.com/zh-cn/library/fw1kt6f9.aspx of Graphics. CopyFromScreen method of c #

Attachment: http://files.cnblogs.com/files/herbertchina/GetFormDemo.zip

Demo: (VS2010 environment)

The image material used in this article:

/// <Summary> /// specify the window area /// </summary> /// <param name = "handle"> window handle. (In a windows application, obtain from the Handle attribute) </param> /// <param name = "rect"> a region in the window </param> /// <returns> </returns> public Bitmap CaptureWindow (IntPtr hWnd, RECT rect) {// get the device context handle IntPtr hscrdc = GetWindowDC (hWnd); // create a memory device context environment (DC) compatible with the specified device) intPtr hmemdc = CreateCompatibleDC (hscrdc); IntPtr myMemdc = CreateCompatibleDC (hscrdc); // return the RECT rect1 of the specified form; GetWindowRect (hWnd, out rect1 ); // return the bitmap region handle IntPtr hbitmap = CreateCompatibleBitmap (hscrdc, rect1.Right-rect1.Left, rect1.Bottom-rect1.Top) corresponding to the specified device environment handle; IntPtr myBitmap = reverse (hscrdc, rect. right-rect. left, rect. bottom-rect. top); // select the bitmap into the memory DC // IntPtr OldBitmap = (IntPtr) SelectObject (hmemdc, hbitmap); SelectObject (hmemdc, hbitmap); SelectObject (myMemdc, myBitmap ); //////////////////////////////////////// //////////////////////////////////////// /start the so-called Painting Process below, this process can be used in many ways, how do you call the API ////////////////////////////////// //////////////////////////////////////// ////// print the form directly to the canvas PrintWindow (hWnd, hmemdc, 0); // IntPtr hw = getdomaintopwindow (); // IntPtr hmemdcClone = GetWindowDC (myBitmap); BitBlt (myMemdc, 0, 0, rect. right-rect. left, rect. bottom-rect. top, hmemdc, rect. left, rect. top, CopyPixelOperation. sourceCopy | CopyPixelOperation. captureBlt); // SelectObject (myMemdc, myBitmap); Bitmap bmp = Bitmap. fromHbitmap (myBitmap); DeleteDC (hscrdc); DeleteDC (hmemdc); DeleteDC (myMemdc); return bmp ;}

Let's explain:

Procedure:
1. Create a device context using the form handle
2. Establish a device-compatible memory device context environment based on the context handle (understood as a canvas or memory)
3. Get the length and width through the form handle, and use CreateCompatibleBitmap to get the bitmap area corresponding to the device environment handle of the form (understood as a beautiful view of nature)
4. using SelectObject, map the bitmap region handle in 3 to the memory device context in 2 (to understand it, I need to draw beautiful scenery of nature, and I need a canvas, the canvas is fixed on the canvas)
5. Plotting, including drawing by various APIs or obtaining graphics
6. This is very important because we don't want the overall image of the past frame, but we want a local image. Therefore, we can take steps 1, 2, 3, and 4, build a canvas (the third step is the difference, that is, the length and width)
7. Copy the previously painted area to our new canvas using the BitBlt method. The BitBlt function is to "Block pixels in the environment area of the specified source device (bit_block) conversion to transfer to the target device Environment"
8. Then it's easy. Bitmap bmp = Bitmap. FromHbitmap (myBitmap); generate a Bitmap image from the painting on the second canvas we copied.
9. Any more operations will follow you.
10. Finally, the most important thing is ...... Do not forget to use the DeleteDC function to clear the context environment created in 1st and 2, because this is an API and there is no Java automatic cleaning mechanism.

In addition:

Furthermore, I am a beginner, and the article is for reference only. If you have any questions, you can refer to Baidu or google. Then, some of the questions I mentioned above must be answered, help me learn things! Thank you!

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.