getpixel

C# Signature:

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

VB.NET Signature

<DllImport("gdi32.dll", SetLastError:=True)> _
Public Function GetPixel(hdc As IntPtr, _
              nXPos As Integer, _
              nYPos as Integer) As UInteger
End Function

VB Signature:

Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long

Alternative Managed API:

using (Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                   Screen.PrimaryScreen.Bounds.Height, 
                   PixelFormat.Format32bppArgb)) 
    {
    using (Graphics g = Graphics.FromImage(bmp)) 
    {
        g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                 Screen.PrimaryScreen.Bounds.Y, 
                 0, 0, 
                 Screen.PrimaryScreen.Bounds.Size, 
                 CopyPixelOperation.SourceCopy);
    }
    return bmp.GetPixel(x, y);
    }

Sample Code:

2nd Example (above did not work for me, here is my solution):

Wrapper Class

Última actualización