2017-10-03 4 views
11

तो मैं किसी दिए गए क्षेत्र में स्क्रीन के बीच में एक निश्चित पैटर्न खोजने की कोशिश कर रहा हूं। मैं AutoItX लाइब्रेरी और PixelSearch विधि का उपयोग कर रहा हूं।मॉनिटर के कुछ क्षेत्र में पिक्सलशर्च

  • आयत एक्स: 1980
  • आयत Y: 630
  • आयत साइज़ एक्स: 1240
  • आयत आकार Y: 180

यह लौटने नहीं कर रहा है कि पैटर्न पाया गया है लेकिन अगर मैं आयत के तारों को 0, 0 पर समायोजित करता हूं तो यह दिखाता है कि पैटर्न पाया गया है।

निम्नलिखित इस्तेमाल किया स्क्रिप्ट:

public void MonsterScan() 
    { 
    if(SixStarMax() == true) 
    { 
     Console.WriteLine("Pattern found"); 
    } 
    } 

    public bool SixStarMax() 
    { 
    Rectangle rect = new Rectangle(1980, 630, 1240, 180); 
    autoSumPoint = AutoItX.PixelSearch(rect, 0xF8F0E0); // 0xF8F0E0 
    autoSumPoint2 = AutoItX.PixelSearch(rect, 0xB7AD9F); // 0xB7AD9F 
    autoSumPoint3 = AutoItX.PixelSearch(rect, 0xCDC6B8); // 0xCDC6B8 
    autoSumPoint4 = AutoItX.PixelSearch(rect, 0x949084); // 0x949084 

    if (rect.Contains(autoSumPoint2) == true && rect.Contains(autoSumPoint2) == true && rect.Contains(autoSumPoint3) == true && rect.Contains(autoSumPoint4) == true) 
    { 
     AutoItX.MouseMove(autoSumPoint.X, autoSumPoint.Y); 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
    } 

संपादित करें:

मेरी पहली स्क्रीन पर cordinates समायोजित करने के लिए कोशिश की और मैं एक त्रुटि फेंक दिया मिलता है।

System.AccessViolationException: 'An attempt was made to read or write to protected memory. This often indicates that other memory is damaged. ' 
+1

इस तरह एक एक्सेसविलेशन अपवाद त्रुटि ऑटोआईट (नेट रैपर से नहीं) के मूल भाग से आता है। आपको ऑटोआईटी निर्माता से संपर्क करने का प्रयास करना चाहिए। –

उत्तर

3

AutoItX 'PixelSearch()a bug है। संभावित समाधान:

  • देखें कि यह latest beta में तय है या नहीं।
  • निर्देशांक बदलें (पुराने संस्करण में एक्स/वाई स्विच किया गया था)।
  • PixelGetColor() का उपयोग करें।
  • एक तृतीय पक्ष image search dll खोजें।
+0

आपके द्वारा सुझाए गए समाधान "एक तृतीय पक्ष छवि खोज डीएल खोजें।", मुझे लगता है कि इसे .NET में काम करने के लिए एक असेंबली की आवश्यकता है? मुझे लगता है कि imagesearch.dll काम नहीं करेगा? – Tweath

0

आप इसे बाहरी पुस्तकालय के बिना कोड कर सकते हैं और बाइट्स को आंतरिक रूप से पढ़कर बहुत तेज़ हो सकते हैं। System.Drawing.Imaging और System.Linq को उपयोग कथन में शामिल करना न भूलें और इसे परियोजना विकल्पों में 'असुरक्षित' विकल्प से संकलित करें।

public bool SixStarMax() 
{ 
    Rectangle rect = new Rectangle(1980, 630, 1240, 180); 

    Bitmap bitmapToScan = GetScreenPart(rect); 

    Point?[] autoSumPoints = new Point?[4]; 

    autoSumPoints[0] = SearchForColor(bitmapToScan, 0xF8F0E0); 
    autoSumPoints[1] = SearchForColor(bitmapToScan, 0xB7AD9F); 
    autoSumPoints[2] = SearchForColor(bitmapToScan, 0xCDC6B8); 
    autoSumPoints[3] = SearchForColor(bitmapToScan, 0x949084); 

    //return true if all points return a value 
    bool containsAll = autoSumPoints.All(p => p.HasValue); 

    if (containsAll) Cursor.Position = autoSumPoints[0].Value; 
    return containsAll; 
} 

public Bitmap GetScreenPart(Rectangle rect) 
{ 
    //initialize bitmap 
    Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 

    //fill bitmap 
    using (Graphics g = Graphics.FromImage(bmp)) 
     g.CopyFromScreen(new Point(rect.Left, rect.Top), new Point(rect.Right, rect.Bottom), rect.Size); 

    return bmp; 
} 

public Point? SearchForColor(Bitmap image, uint color) 
{ 
    Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); 

    BitmapData data = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb); 

    //works for 32-bit pixel format only 
    int ymin = rect.Top, ymax = Math.Min(rect.Bottom, image.Height); 
    int xmin = rect.Left, xmax = Math.Max(rect.Right, image.Width) - 1; 

    int strideInPixels = data.Stride/4; //4 bytes per pixel 
    unsafe 
    { 
     uint* dataPointer = (uint*)data.Scan0; 
     for (int y = ymin; y < ymax; y++) 
      for (int x = xmin; x < xmax; x++) 
      { 
       //works independently of the data.Stride sign 
       uint* pixelPointer = dataPointer + y * strideInPixels + x; 
       uint pixel = *pixelPointer; 
       bool found = pixel == color; 
       if (found) 
       { 

        image.UnlockBits(data); 
        return new Point(x, y); 

       } 
      } 
    } 
    image.UnlockBits(data); 
    return null; 
} 
संबंधित मुद्दे