2017-08-14 17 views
0
 public Point PixelSearchPoint(Bitmap b, Color PixelColor, int Shade_Variation) 
    { 


     Color Pixel_Color = PixelColor; 

     Point Pixel_Coords = new Point(-1, -1); 
     using (Bitmap RegionIn_Bitmap = (Bitmap)b.Clone()) 
     { 
      BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 

      int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr 

      unsafe 
      { 
       for (int y = 0; y < RegionIn_BitmapData.Height; y++) 
       { 
        byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride); 

        for (int x = 0; x < RegionIn_BitmapData.Width; x++) 
        { 
         if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue 
         { 
          if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green 
          { 
           if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red 
           { 
            Pixel_Coords = new Point(x, y); 
            RegionIn_Bitmap.Dispose(); 
            goto End; 

           } 
          } 
         } 
        } 
       } 
      } 
     } 
     End: 
     b.Dispose(); 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     return Pixel_Coords; 
    } 

cette recherche de code à partir du code couleur trouver bitmapcsharp dangereux BitmapData Access Memory Leak

using (Bitmap b = new Bitmap(NativeHelper.GetOriginalWinScreen(handle))) 
          { 


           Point p = PixelSearchPoint(b, c, 0); 
           if (p.X != -1 && p.Y != -1) 
           { 
            Logwrite(p.X + "/" + p.Y + " Click With" + c.ToString()); 
            chk = true; 
            ClickPos(p.X, p.Y); 
            notecomplete = true; 
            counttest++; 
           } 

           b.Dispose(); 

          } 

cette mémoire de code de fuite si rapide lorsque l'accès méthode dangereuse GC.Collect et GC.WaitForPendingFinalizers(); Ne fonctionne pas sur la méthode UnSafe ??

  1. utilisant GDI + capture et cette méthode ne raison de MemoryLeak
  2. en utilisant foreach Trouver point de couleur en utilisant la méthode dangereuse < < fuite de mémoire de 2 Go Plus de mémoire et garder un message de ligne de débogage OutOfMemoryException

Répondre

0

Si vous appelez RegionIn_Bitmap.LockBits, vous devez vous assurer que vous appelez UnlockBits avant de disposer.

+0

'Pixel_Coords = nouveau Point (x, y); b.UnlockBits (RegionIn_BitmapData); b.Dispose(); RegionIn_Bitmap.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); goto Fin; 'Toujours pas de travail .. – cherry

+0

vraiment utile merci – cherry