Skip to content

Instantly share code, notes, and snippets.

@Taiizor
Last active December 31, 2021 13:35
Show Gist options
  • Save Taiizor/34f567837fe1529cfce4524a7cd44c71 to your computer and use it in GitHub Desktop.
Save Taiizor/34f567837fe1529cfce4524a7cd44c71 to your computer and use it in GitHub Desktop.
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetWindowDC(IntPtr window);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("user32.dll", SetLastError = true)]
private static extern int ReleaseDC(IntPtr window, IntPtr dc);
private static Color GetColor(string x, string y)
{
return GetColor(Convert.ToInt32(x), Convert.ToInt32(y));
}
private static Color GetColor(int x, int y)
{
IntPtr desk = GetDesktopWindow();
IntPtr dc = GetWindowDC(desk);
int a = (int)GetPixel(dc, x, y);
ReleaseDC(desk, dc);
return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}
private void SearchPixel(string hexcode = "#BD20B1", int xspixel = 0, int xepixel = 1920, int yspixel = 0, int yepixel = 1080)
{
int hexCount = 0;
Color desiredPixelColor = ColorTranslator.FromHtml(hexcode);
Parallel.For(xspixel, xepixel, (x, xstate) =>
{
Parallel.For(yspixel, yepixel, (y, ystate) =>
{
Color currentPixelColor = GetColor(x, y);
if (desiredPixelColor == currentPixelColor)
{
hexCount++;
LblToplamHex.Content = hexCount.ToString();
}
});
});
LblToplamHex.Content += " - End..";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment