簡體   English   中英

如何點擊C#中截圖的特定像素

[英]How to click on specific pixel of screenshot in C#

我正在嘗試通過不停地截取它的屏幕截圖並掃描屏幕截圖並嘗試通過檢查它的 RGB 顏色並模擬點擊來找到特定像素來為游戲制作一個機器人。

如何模擬對特定像素的點擊?

謝謝

我已經完成了屏幕截圖部分

// Take the screenshot
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size);

// Scan the screenshot to find the desired pixel
Color pixelColor = screenshot.GetPixel(x, y);

// Check if the pixel has the desired color
if (pixelColor.R == r && pixelColor.G == g && pixelColor.B == b)
{
    // Set the cursor's position to the desired pixel
    System.Drawing.Point cursorPos = new System.Drawing.Point(x, y);
    Cursor.Position = cursorPos;

    // Simulate a mouse click
    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

在這個例子中,x和y是你想要點擊的屏幕截圖上像素的坐標,r,g和b分別是想要的像素的紅色、綠色和藍色值。 您可以調整這些值以將光標的 position 設置為所需的像素並檢查所需的顏色。

注意:此示例分別使用 System.Drawing 和 user32.dll 庫中的 GetPixel 和 mouse_event 方法來模擬鼠標單擊。 這些方法不是 C# 語言的一部分,因此您需要添加對 System.Drawing 和 user32.dll 庫的引用,並在代碼中包含 System.Drawing 和 System.Runtime.InteropServices 命名空間才能使用它們。 您可以在 Microsoft 文檔中找到有關如何執行此操作的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM