2016-12-19 1 views
0

주 화면을 캡쳐하고 비트 맵으로 변환 한 다음 비트 맵 "키"로 스크린 샷 비트 맵을 검색하는 프로그램을 작성 중입니다. 비트 맵 "키"를 전체 그림과 비교하여 일치하는 위치를 찾은 다음 커서를 위치로 이동 한 다음 마우스를 클릭합니다. bmpLogin이 (가) null 인 문제가 있습니다. Mopar/Mopar/Resources/bmpLogin.bmp에 비트 맵 키가 있습니다. 비교를 위해이 이미지를 찾을 수없는 것 같습니다. 다음은 관련 코드입니다.다른 비트 맵 내의 비트 맵 위치 찾기

시작 버튼 :

private void startBotButton(object sender, EventArgs e) 
    { 
     //Screenshot 
     //This is where we will be writing the loop for the bot 
     Bitmap bmpScreenshot = Screenshot(); 

     //Set the background of the form the screenshot that was taken 
     this.BackgroundImage = bmpScreenshot; 
     //Find the node and check if it exists within the screenshot 
     Point location; 
     bool success = FindBitmap(Properties.Resources.bmpLogin, bmpScreenshot, out location); 

     //Check if it found the node in the bitmap 
     if (success == false) 
     { 
      MessageBox.Show("Couldn't find the node"); 
      return; 
     } 

     //Move the mouse to the node 
     Cursor.Position = location; 

     //click 
     MouseClick(); 
    } 

스크린 샷 : 비트 맵을 검색

//Screenshot the screen 
    private Bitmap Screenshot() 
    { 
     //This is where we will store the screenshot 
     Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 

     //Create a graphics object so we can draw the screen in the bitmap 
     Graphics g = Graphics.FromImage(bmpScreenshot); 

     //Copy from screen to bitmap 
     g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size); 

     //Return the screenshot 
     return bmpScreenshot; 
    } 

:

/// <summary> 
    /// Find the location of a bitmap within another bitmap and return if it was successfully found 
    /// </summary> 
    /// <param name="bmpNeedle">The image we want to find</param> 
    /// <param name="bmpHaystack">Where we want to search for the image</param> 
    /// <param name="location">Where we found the image</param> 
    /// <returns>If the bmpNeedle was found successfully</returns> 
    private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location) 
    { 
     if (bmpNeedle == null || bmpHaystack == null) 
     { 
      location = new Point(); 
      return false; 
     } 
     for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++) 
     { 
      for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++) 
      { 
       for (int innerX = 0; innerX < bmpNeedle.Width; innerX++) 
       { 
        for (int innerY = 0; innerY < bmpNeedle.Height; innerY++) 
        { 
         Color cNeedle = bmpNeedle.GetPixel(innerX, innerY); 
         Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY); 

         if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B) 
         { 
          goto notFound; 
         } 
        } 
       } 
       location = new Point(outerX, outerY); 
       return true; 
      notFound: 
       continue; 
      } 
     } 
     location = Point.Empty; 
     return false; 
    } 
+0

첫 번째 * GetPixel *은 너무 느립니다. * 잠금 - 비트 맵을 잠금 해제하십시오. 둘째로 @DevNull이 말하는 것. –

답변

0

두려워 내 대표 단지 의견을 너무 낮습니다. 그래서 불완전한 대답에 대한 사과

당신은 더 확실하게 대답 할만큼 충분하지 않습니다 (우리는 당신의 리소스가 프로젝트에 어떻게 포함되어 있는지를 볼 필요가 있습니다). 그러나 여기에 대한 좋은 대답은 귀하의 문제에 대한 다양한 근원 : https://social.msdn.microsoft.com/Forums/vstudio/en-US/a4ace5de-9376-44d0-a453-1f1c39c47a86/where-did-the-propertiesresources-go?forum=csharpgeneral

가독성을 높이고 (고토를 방지하기 위해), 나는 내부의 두 루프를 함수로 옮기는 것을 고려할 것입니다.

마지막으로 (그리고 아마도 가장 중요한 것은) 성능면에서 항상 외부 루프에서 y를 반복하고 내부 루프에서 x를 반복하려고합니다. 상당한 크기의 이미지에서 상당한 속도 향상을 느껴야합니다.