2010-11-17 44 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