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.