title: How to get all the pixel values inside a rectangle in C#
body: <p>I'm developing a program to get all the pixels inside a rectangle. There is an image control and the user can click on a part inside it. When the user clicks on a particular location, a rectangle is drawn. I would like to get all the pixels inside that rectangle. I'm getting to draw the rectangle now. But i'm not able to get all the pixel values. Please find the code snippet for drawing the rectangle below.</p>
  
  <pre><code>private void panel1_Paint(object sender, PaintEventArgs e)
      {
          foreach (var rectKey in rectangles.Keys)
          {
              using (var pen = new Pen(rectKey))     //Create the pen used to draw the rectangle (using statement makes sure the pen is disposed)
              {
                  //Draws all rectangles for the current color
                  //Note that we're using the Graphics object that is passed into the event handler.
                  e.Graphics.DrawRectangles(pen, rectangles[rectKey].ToArray()); 
              }
          }
      }
  
      private void panel1_MouseUp(object sender, MouseEventArgs e)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Left)
          {
              Color c = System.Drawing.Color.GreenYellow ;     //Gets a color for which to draw the rectangle
  
              //Adds the rectangle using the color as the key for the dictionary
              if (!rectangles.ContainsKey(c))
              {
                  rectangles.Add(c, new List&lt;Rectangle&gt;());
              }
              rectangles[c].Add(new Rectangle(e.Location.X - 12, e.Location.Y - 12, 25, 25));    //Adds the rectangle to the collection
          }
          //Make the panel repaint itself.
         panel1.Refresh();// Directs to panel1_Paint() event
         rectangles.Clear();
      }
  </code></pre>