1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Exception Thrown when trying to capture a screenshot

Discussion in 'C#' started by drake10k, Aug 30, 2010.

  1. drake10k

    drake10k New Member

    Joined:
    Aug 30, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I'm receiving an error message (ArgumentException was unhandled - Parameter is not valid) when I'm trying to capture a portion of the screen with a rectangle.

    It works great when drawing the rectangle with clickPoint.X < lastPoint.X and clickPoint.Y < lastPoint.Y, but when lastPoint.X or/and lastPoint.Y is lower than clickPoint.X or/and clickPoint.Y it throws the exception mentioned above.

    Here is the code:

    Code:
     public partial class captureForm : Form
        {
            public bool mouseBtnClicked;
    
            public Point clickPoint = new Point();
            public Point lastPoint = new Point();
            
            public Bitmap areaCapture;
    
            public captureForm()
            {
                InitializeComponent();
                formPropreties();
                backGround();
    
                this.MouseDown += new MouseEventHandler(mouse_Click);
                this.MouseUp += new MouseEventHandler(mouse_Up);
                this.MouseMove += new MouseEventHandler(mouse_Move);
                mouseBtnClicked = false;
            }
    
            public void formPropreties()
            {
                this.ControlBox = false;
                this.Text = string.Empty;
                this.ShowInTaskbar = false;
                this.WindowState = FormWindowState.Maximized;
                this.Cursor = Cursors.Cross;
                this.DoubleBuffered = true;
            }
    
            public void backGround()
            {
                Size screenBounds = Screen.PrimaryScreen.Bounds.Size;
                Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height);
                Graphics W = Graphics.FromImage(capture);
                W.CopyFromScreen(Point.Empty, Point.Empty, screenBounds);
                this.BackgroundImage = capture;
            }
    
            private void mouse_Click(object sender, MouseEventArgs e)
            {
                mouseBtnClicked = true;
                clickPoint.X = e.X;
                clickPoint.Y = e.Y;
                lastPoint.X = -1;
                lastPoint.Y = -1;
            }
    
            public void drawnRectangle(Point p1, Point p2)
            {
                Rectangle rc = new Rectangle();
    
                p1 = PointToScreen(p1);
                p2 = PointToScreen(p2);
    
                if (p1.X < p2.X)
                {
                    rc.X = p1.X;
                    rc.Width = p2.X - p1.X;
                }
                else
                {
                    rc.X = p2.X;
                    rc.Width = p1.X - p2.X;
                }
    
                if (p1.Y < p2.Y)
                {
                    rc.Y = p1.Y;
                    rc.Height = p2.Y - p1.Y;
                }
                else
                {
                    rc.Y = p2.Y;
                    rc.Height = p1.Y - p2.Y;
                }
                ControlPaint.DrawReversibleFrame(rc, Color.Black, FrameStyle.Dashed);
            }
    
            public void mouse_Up(object sender, MouseEventArgs e)
            {
                mouseBtnClicked = false;
                if (lastPoint.X != 1)
                {
                    Point currentPoint = new Point(e.X, e.Y);
                    drawnRectangle(clickPoint, lastPoint);
                }
    
                Point StartPoint = new Point(clickPoint.X + 1, clickPoint.Y + 1);
                Point hideBorder = new Point( - 1, - 1);
                Rectangle rc = new Rectangle(clickPoint.X, clickPoint.Y, lastPoint.X - clickPoint.X, lastPoint.Y - clickPoint.Y);
                areaCapture = new Bitmap(rc.Width, rc.Height);
                using (Graphics c = Graphics.FromImage(areaCapture))
                {
                    c.CopyFromScreen(StartPoint, hideBorder, rc.Size);
                }
                DialogResult = DialogResult.OK;
            }
    
            private void mouse_Move(object sender, MouseEventArgs e)
            {
                Point currentPoint = new Point(e.X, e.Y);
                if (mouseBtnClicked)
                {
                    if (lastPoint.X != -1)
                    {
                        drawnRectangle(clickPoint, lastPoint);
                    }
                    lastPoint = currentPoint;
                    drawnRectangle(clickPoint, currentPoint);
                }
            }
        }
    Thank you
     

Share This Page