2010-11-17 55 views

回答

0

您可以使用WINAPI GetPixel(...)

5

使用Graphics.CopyFromScreen复制一个1x1的位图,Bitmap.GetPixel()来获得它的颜色。

+0

谢谢。这似乎是一个简单的解决方案。 – 2010-11-17 12:05:34

1

首先,捕捉屏幕。

Rectangle screenRegion = Screen.AllScreens[0].Bounds; 
Bitmap screen = new Bitmap(screenRegion.Width, screenRegion.Height, PixelFormat.Format32bppArgb); 

Graphics screenGraphics = Graphics.FromImage(screenBitmap); 
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size); 

Then,get the pixel from the bitmap。

+0

请注意,它只适用于主监视器。如果您需要从其他显示器捕捉像素,则必须在代码的第一行更改零索引。 – 2010-11-17 12:03:25

2

首先导入这些DLL

[DllImport("user32.dll")]  
    static extern IntPtr GetDC(IntPtr hwnd); 

    [DllImport("user32.dll")] 
    static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc); 

    [DllImport("gdi32.dll")] 
    static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); 

然后写该方法GetPixelColor(X,Y);

 static public System.Drawing.Color GetPixelColor(int x, int y) 
     { 
     IntPtr hdc = GetDC(IntPtr.Zero); 
     uint pixel = GetPixel(hdc, x, y); 
     ReleaseDC(IntPtr.Zero, hdc); 
     Color color = Color.FromArgb((int)(pixel & 0x000000FF), 
        (int)(pixel & 0x0000FF00) >> 8, 
        (int)(pixel & 0x00FF0000) >> 16); 
     return color; 
     } 

调用方法Color clr = GetPixelcolor(50,50);

+0

哇。谢谢。这正是我期待的。 – 2010-11-17 12:28:38