C # Region screenshot--Call API screenshot

Source: Internet
Author: User

Original: C # region--Invoke API

Preface: For a C + + developer is nothing but a piece of cake, also have friends use C # Graphics.copyfromscreen method screen operation, as a day thinking of C + + development Game of the Junior c#.net programmer, I just own research area, failed n many times, Finally in the half of the dream to get out of the simple demo, simply share it. So swim, really unbearable, in addition to my C + + level is in the entry level, if the blog has any problems, please friends message correct, thank you for your attention!

Annex and other:

The Graphics.copyfromscreen method of C # https://msdn.microsoft.com/zh-cn/library/fw1kt6f9.aspx

Accessories: Http://files.cnblogs.com/files/herbertchina/GetFormDemo.zip

This article Demo: (VS2010 Environment)

The picture material used in this article:

Topics in this article:

Take the desired part from the picture of the form, this example is the Captcha area.

Body:

Post the main code first

        /// <summary>        ///Specify window area/// </summary>        /// <param name= "Handle" >The window handle. (in Windows applications, obtained from the handle property)</param>        /// <param name= "rect" >an area in the window</param>        /// <returns></returns>         PublicBitmap CaptureWindow (IntPtr hwnd,rect RECT) {//Get device context handleIntPtr HSCRDC =GETWINDOWDC (HWND); //creates a memory device context (DC) that is compatible with the specified deviceIntPtr HMEMDC =CreateCompatibleDC (HSCRDC); INTPTR MYMEMDC=CreateCompatibleDC (HSCRDC); //returns the rectangle size of the specified formRECT Rect1; GetWindowRect (HWnd, outrect1); //returns the bitmap area handle corresponding to the specified device environment handleIntPtr hbitmap = CreateCompatibleBitmap (HSCRDC, rect1. Right-rect1. Left, Rect1. Bottom-rect1.            TOP); INTPTR Mybitmap= CreateCompatibleBitmap (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); /////////////////////////////////////////////////////////////////////////////            //            //The following is the beginning of the so-called painting process, this process can be used a lot of ways to see how you call the API//            /////////////////////////////////////////////////////////////////////////////            //print a form directly to the canvasPrintwindow (HWnd, HMEMDC,0); //IntPtr HW = GetDesktopWindow (); //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); returnbmp; }

Explain it:

Principle steps:
1. Create a device context through form handle
2, according to the context of the environment handle the corresponding device-compatible memory device context (understood as a piece of artboard, in memory)
3, through the form handle to get the long width, through the createcompatiblebitmap to get the form corresponding device environment handle corresponding to the bitmap region (understanding as a beautiful landscape of nature)
4, through the SelectObject directivity, the 3 in the bitmap area handle corresponds to the memory device in the context of 2 (understanding, I need to draw this beautiful scenery of nature, I need to have a corresponding canvas, here is the canvas on the fixed)
5, drawing, including various API drawings or get graphics
6, here is very important, because we are not to the past framework of the overall graphics, we want to be local, so we can through 1, 2, 3, 4 steps, and then build a canvas (the third step is a little different, is the length of width)
7, through the BitBlt method to copy an area of a previously painted painting to our new artboard, the function of BitBlt is "to the specified source device environment area of pixels (bit_block) conversion to transfer to the target device environment"
8, then simple, Bitmap bmp = Bitmap.fromhbitmap (MYBITMAP); Create a bitmap image from the picture on the second artboard that we copied
9. What to do with you
10. Finally, finally, the most important thing is ... Do not forget to use the DELETEDC function to clear the context of the 1th, 2, because this is the API, no Java automatic cleanup mechanism

Furthermore:

Moreover, the man I am a beginner, the article is for reference only, what question can Baidu, Google, then is, I said above must have the question, please see the friend of the understanding to make a message, help the small friends study one or two! Thank you!

C # Zone--Invoke API

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.