0

I'm using WinForms. In my form i have a picturebox with an image. How can i paint the picturebox but not the area inside the expanding square. Here is my code. Currently i could create the expanding square but i don't know how to paint the picturebox white outside of that square.

    int _cropX, _cropY, _cropWidth, _cropHeight;
    private State _currentState;

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (Crop_Checkbox.Checked == true)
        {
            if (_currentState == State.Crop)
            {
                Cursor = Cursors.Cross;
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    //X and Y are the coordinates of Crop
                    pictureBox1.Refresh();
                    _cropWidth = e.X - _cropX;
                    _cropHeight = e.Y - _cropY;
                    pictureBox1.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _cropWidth, _cropHeight);
                }

            }
        }
        else
        {
            Cursor = Cursors.Default;
        }
    }

    private void Crop_Checkbox_CheckedChanged(object sender, EventArgs e)
    {
        if (Crop_Checkbox.Checked == true)
        {
            this.Cursor = Cursors.Cross;
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (Crop_Checkbox.Checked == true)
        {
            if (_currentState == State.Crop)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    Cursor = Cursors.Cross;
                    _cropX = e.X;
                    _cropY = e.Y;

                    _cropPen = new Pen(Color.FromArgb(153, 180, 209), 3); //2 is Thickness of line

                    _cropPen.DashStyle = DashStyle.DashDotDot;
                    pictureBox1.Refresh();
                }
            }
        }
        else
        {
            Cursor = Cursors.Default;
        }
    }

    public Pen _cropPen;

    private enum State
    {
        Crop
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {

        if (Crop_Checkbox.Checked == true)
        {
          //Paint picturebox...

        }
        else
        {
            Cursor = Cursors.Default;
        }

   }

enter image description here

taji01
  • 2,527
  • 8
  • 36
  • 80

1 Answers1

1

This is best done with a GraphicsPath:

enter image description here

using System.Drawing.Drawing2D;
..

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Rectangle r1 = pictureBox1.ClientRectangle;  // note I don't use width or height!
    Rectangle r2 = new Rectangle(50, 30, 80, 40);
    GraphicsPath gp = new GraphicsPath(FillMode.Alternate);
    gp.AddRectangle(r1);  // first the big one
    gp.AddRectangle(r2);  // now the one to exclude
    e.Graphics.FillPath( Brushes.Gold, gp);
}

Note that I..

  • ..use the Paint event for persistent grphics
  • ..only paint onto the surface of the PictureBox, not into its image. See here for the difference!
  • You can add more rectangles or other shapes to exclude.

If you want image and surface combined, either draw into the image or ask the PictureBox to DrawToBitmap..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111