title: c# get rectangle bounds from image
body: <p>lets say i have a image <a href="https://i.stack.imgur.com/ODsed.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ODsed.jpg" alt="enter image description here"></a></p>
  
  <p>i would like to find the black rectangle bounds(right,left,width,height) in the image(lets say there's no other black pixels in this image).
  my code so far is:</p>
  
  <pre><code>    private unsafe Bitmap GetDiffBitmap(Bitmap bmp)
          {
  
  
              bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
  
              IntPtr scan0 = bmData.Scan0;
  
  
              int stride = bmData.Stride;
  
  
              int nWidth = bmp.Width;
              int nHeight = bmp.Height;
  
              for(int y = 0; y &lt; nHeight; y++)
  
              {
                  //define the pointers inside the first loop for parallelizing
                  byte* p = (byte*)scan0.ToPointer();
                  p += y * stride;
  
  
  
                  for (int x = 0; x &lt; nWidth; x++)
                  {
                      //always get the complete pixel when differences are found
  
                      if(p[0]==0 &amp;&amp; p[1]==0 &amp;&amp; p[2]==0)
                      { 
                             // p[0] = 255;
                             // p[1] = 255;
                             // p[2] =255;
  
                              right = nWidth;//geting the position of the lastest black pixel;
  
                        }  
  
  
  
                      p += 4;
  
  
                  }
  
              }
  
  
  
              bmp.UnlockBits(bmData);
  
  
              return bmp;
          }
  </code></pre>
  
  <p>its seems like my nwidth is also as the image width-so its not working.. i got acces to these pixels and i can change them but i dont know why i can count them and find a proper bounds of the black rectangle... if anyone could help me i would really apperciate it,</p>