The code the link is not working. I can't able to run the code

Is possible to capture the rectangle using array and able to edit the position. I tried with array and it's shows error message as ("Object reference not set to an instance of an object." (Null reference Exception was unhandled).

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (mybitmap == null)
            {
                mybitmap = new Bitmap(sz.Width, sz.Height);
                
            }
            rect[count] = new Rectangle(e.X, e.Y, 0, 0);
            this.Invalidate();
              
                
        }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
       {

           if (stayToolStripMenuItem.Checked == true)
           {
               switch (e.Button)
               {
                   case MouseButtons.Left:
                       {
                           rect[count] = new Rectangle(rect[count].Left, rect[count].Top, e.X - rect[count].Left, e.Y - rect[count].Top);
                           pictureBox1.Invalidate();
                            break;
                       }

                      
               }
           }
            
        }

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (stayToolStripMenuItem.Checked == true)
            {
                button1.Visible = true;
                button2.Visible = true;
                
                if (mybitmap == null)
                {
                    return;
                }


                using (g = Graphics.FromImage(mybitmap))
                {

                    using (Pen pen = new Pen(Color.Red, 2))
                    {
                        //g.Clear(Color.Transparent);


                        e.Graphics.DrawRectangle(pen, rect);
                        label1.Top = rect[count].Top; label1[count].Left = rect[count].Left; label1.Width = rect[count].Width;
                        label1.Height = rect[count].Height;

                        
                            if (label1.TextAlign == ContentAlignment.TopLeft)
                            {
                                e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect);
                                g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect);
                                g.DrawRectangle(pen, rect[count]);

                            }
}
}
}
}

How can i do this.......

Have stored all drawn rectangle in list<rectangle>....

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            
                
                if (mybitmap == null)
                {
                    mybitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
                    mybitmap.SetResolution(300, 300);
                }


                
                    rect = new Rectangle(e.X, e.Y, 0, 0);
                   

        }



        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
                if (mybitmap == null)
                {
                    return;
                }

                
                    using (g = Graphics.FromImage(mybitmap))
                    {
                    using (Pen pen = new Pen(Color.Green, 2))
                    {
                       
                        
                            foreach (Rectangle r in rectangles)
                            {
                                e.Graphics.DrawRectangle(pen, r);

                                
                            }

                         }
                    }
        }

                   

           

        }


private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            
                    rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
                   
                    
                        rectangles.Add(rect);
                        pictureBox1.Invalidate();
		     

               

            
        }
This question has already been answered. Start a new discussion instead.