如何获取屏幕指定坐标的颜色或者鼠标位置的屏幕颜色...请各位帮帮忙...

解决方案 »

  1.   

    http://search.csdn.net/Expert/topic/1871/1871865.xml?temp=.9658167
      

  2.   

    [DllImport("gdi32.dll")]
    static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos); [DllImport("gdi32.dll")]
    static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);

    [DllImport("gdi32.dll")]
    static public extern bool DeleteDC(IntPtr DC); static public byte GetRValue(uint color)
    {
    return (byte)color;
    } static public byte GetGValue(uint color)
    {
    return ((byte)(((short)(color)) >> 8));
    } static public byte GetBValue(uint color)
    {
    return ((byte)((color)>>16));
    } static public byte GetAValue(uint color)
    {
    return ((byte)((color)>>24));
    } static public Color GetColorOfScreen(Point screenPoint)
    {
    IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero);
    uint colorref = GetPixel(displayDC, screenPoint.X, screenPoint.Y);
    DeleteDC(displayDC);
    byte Red = GetRValue(colorref);
    byte Green = GetGValue(colorref);
    byte Blue = GetBValue(colorref);
    return  Color.FromArgb(Red, Green, Blue);
    }这段代码很简单,听起来很复杂的东西,其实很简单的。
    我看过论坛上的许多回帖,不少有点走弯路,还要 BitBlt,还要 Bitmap,其实都是不必的。
    而且忽略了 GetPixel 获取的 uint 值与 Color 的四个分量之间的对应关系。
    Color.ToARGB() 结果的顺序是 ARGB,而 GetPixel() 返回的是 ABGR。