Retrieve the color value of a pixel on the screen

Source: Internet
Author: User

To retrieve the color of a certain pixel on the screen, the GDI function getpixel is often used. Its prototype is as follows: Colorref getpixel ( HDC HDC ,// Handle to DC Int Nxpos ,// X-coordinate of pixel Int Nypos// Y-coordinate of pixel );The efficiency of getpixel is not high. frequent use of getpixel may result in a software response speed. For example, the drawing board tool is generally a single-threaded architecture. If the program is doing the anti-sawtooth optimization of the just-drawn lines, the user cannot continue to draw lines, therefore, the code execution speed of algorithms that require anti-aliasing is very high. In the anti-sawtooth algorithm, the pixel color is required. If you do not need the getpixel function, can you use another method to obtain the color value of a certain pixel? Yes. The idea is as follows: Obtain the bitmap information in the canvas and calculate the position corresponding to the pixel in-place graph to obtain the color value. The following functions only consider 32 bitmaps: hbitmap getsrcbit (HDC, DWORD bitwidth, DWORD bitheight) {HDC hbufdc; hbitmap, hbittemp; // create a device context (HDC) hbufdc = createcompatibledc (HDC); // create hbitmap = createcompatiblebitmap (HDC, bitwidth, bitheight); hbittemp = (hbitmap) SelectObject (hbufdc, hbitmap ); // obtain the bitmap buffer stretchblt (hbufdc, 0, 0, bitwidth, bitheight, HDC, 0, 0, bitwidth, bitheight, srccopy); // obtain the final bitmap information HB Itmap = (hbitmap) SelectObject (hbufdc, hbittemp); // release the memory deleteobject (hbittemp);: deletedc (hbufdc); Return hbitmap ;} ///////////// Bitmap bitmap; // hbitmap hbmpdc = getsrcbit (HDC, dcrect. right, dcrect. bottom + 50); // The height must be greater than the actual height to avoid cross-border GetObject (hbmpdc, sizeof (Bitmap), (lpstr) & Bitmap); void * lpbmpvoid = globalalloc (ghnd, bitmap. bmheight * bitmap. bmwidthbytes); lpstr lpbmpstr = (lpstr) glo Ballock (lpbmpvoid); getbitmapbits (hbmpdc, bitmap. bmwidthbytes * bitmap. bmheight, lpbmpstr );............... .............. Offset = (bitmap. bmwidthbytes )* Nypos+ Nxpos* (Bitmap. bmbitspixel/8); bgcolor = (* (DWORD *) (lpbmpstr + offset); bgcolor = (bgcolor & 0xff00ff00) | (bgcolor> 16) & 0x000000ff) | (bgcolor <16) & 0x00ff0000 );............................. Globalunlock (lpbmpvoid); globalfree (lpbmpvoid); deleteobject (hbmpdc); // The bgcolor is the color value.

 

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.