This article mainly obtains the pixel color of the current location of the mouse. Speaking of this, you may still remember the straw function in PhotoShop, that is, you can click a position in the image to get the color of the pixel at that position. The function of this program is similar to that of the straw. Let's take a look at the operation:
The Code is as follows:
Class CallWin32GDIAPI {private const int SM_CXSCREEN = 0x00000000; // The screen's abscissa private const int SM_CYSCREEN = 0x00000001; // The ordinate private const int SRCCOPY = 0x00CC0020; // grating operation 1 parameter [DllImport ("GDI32.dll", EntryPoint = "DeleteDC")] public static extern IntPtr DeleteDC (IntPtr hdc); [DllImport ("GDI32.dll ", entryPoint = "DeleteObject")] public static extern IntPtr DeleteObject (IntPtr hObject); [DllImport ("GDI3 2. dll ", EntryPoint =" BitBlt ")] public static extern IntPtr BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHight, IntPtr dhcSRC, int nXSRC, int nYSRC, int dwRop); [DllImport ("GDI32.dll", EntryPoint = "CreateCompatibleBitmap")] public static extern IntPtr CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight ); [DllImport ("GDI32.dll", EntryPoint = "CreateCompatibleDC")] public static Extern IntPtr CreateCompatibleDC (IntPtr hdc); [DllImport ("GDI32.dll", EntryPoint = "SelectObject")] public static extern IntPtr SelectObject (IntPtr hdc, IntPtr hgdiobj ); [DllImport ("User32.dll", EntryPoint = "getshorttopwindow")] public static extern IntPtr getshorttopwindow (); [DllImport ("User32.dll", EntryPoint = "GetDC")] public static extern IntPtr GetDC (IntPtr hWnd); [DllImport ("User32.dll", Entr YPoint = "GetSystemMetrics")] public static extern IntPtr GetSystemMetrics (int nIndex); [DllImport ("User32.dll", EntryPoint = "ReleaseDC")] public static extern IntPtr ReleaseDC (IntPtr hWnd, intPtr hdc); // <summary> // returns the color of the pixel /// </summary> /// <returns> </returns> internal static Bitmap GetDesktop () {// screen resolution width and height, in pixels int Xscreen; int Yscreen; IntPtr hBmp; // retrieves the specified window handle IntPtr hdcScreen = GetDC (G Etemeditopwindow (); // create a device-compatible internal device context environment IntPtr hdcCompatible = CreateCompatibleDC (hdcScreen); // obtain the screen resolution width and height, xscreen = (int) GetSystemMetrics (SM_CXSCREEN); Yscreen = (int) GetSystemMetrics (SM_CYSCREEN); // create a device-compatible bitmap hBmp = CreateCompatibleBitmap (hdcScreen, xscreen, Yscreen); if (hBmp! = IntPtr. zero) {// specifies the bitmap object to the relevant device context environment. IntPtr hOldBmp = (IntPtr) SelectObject (hdcCompatible, hBmp ); // BitBlt (hdcCompatible, 0, 0, Xscreen, Yscreen, hdcScreen, 0, 0, SRCCOPY) for bitmap pixels; // SRCCOPY: copy the source rectangle area directly to the target rectangle area SelectObject (hdcCompatible, hOldBmp); // release the system resource DeleteDC (hdcCompatible); ReleaseDC (getshorttopwindow (), hdcScreen ); // create Bitmap object Bitmap bmp = System Based on the returned handle. drawing. image. fromHbitmap (hBmp); DeleteObject (hBmp); // force garbage collection GC. collect (); return bmp;} return null ;}}
Finally, I hope to reprint friends can respect the author's labor results, plus reprint address: http://www.cnblogs.com/hanyonglu/archive/2011/04/12/2014248.html thank you.
Complete. Pai_^