3 Replies - 2684 Views - Last Post: 26 June 2011 - 02:38 PM Rate Topic: -----

#1 hyphen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-June 11

Parameter is not valid error while drawing a rectangle

Posted 26 June 2011 - 01:14 PM

Hello All, I hope that someone would be able to help me.

I have made a program that lets you select an area of a picture and then crop it, I am having problems while drawing the rectangle that shows which area will be cropped, following is my code.

    Dim CanvasGraphics As Graphics
    Dim FirstLeft As Integer
    Dim FirstTop As Integer
    Dim CropWidth As Integer
    Dim CropHeight As Integer
    Dim MouseDn As Boolean = False

This is declaration of all the variables that I am using.

    Private Sub Canvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Canvas.Paint
        CanvasGraphics = e.Graphics
    End Sub

Over here I send the graphics handle of the "canvas" control (which is just a picturebox) to a higher level, so I can use it later.

    Private Sub Canvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Canvas.MouseDown
        FirstLeft = e.X
        FirstTop = e.Y
        MouseDn = True
    End Sub

Over here I get the starting position for my rectangle

    Private Sub Canvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Canvas.MouseMove
        If MouseDn = True Then
            CropWidth = e.X - FirstLeft
            CropHeight = e.Y - FirstTop

            If CropWidth > 4 And CropHeight > 4 Then
                CanvasGraphics.DrawRectangle(Pens.Red, FirstLeft, FirstTop, CropWidth, CropHeight)
            End If
        End If
    End Sub

This part is supposed to draw the rectangle, as I am moving the mouse over the Canvas control, but I get an error where the rectangle is being drawn, I originally thought that the problem might have been with the fact that as soon as I begin moving the mouse, the CropWidth and CropHeight variables are at zero, and that is causing the invalid input error. So I made it that it would not draw the rectangle until it reached a minimum size of 4 x 4 pixels but it still does not fix the error, I debugged to see if there was a problem with the values of the variables, but they all seemed fine, I even tries doing this
CanvasGraphics.DrawRectangle(Pens.Red, 20, 20, 20, 20)

so as to eliminate the factor of the wrong values, but still the same error.

It would be really great if someone here could point out what may be wrong with this, or maybe guide me to a better solution.

I was thinking that if this did not work I would use Panels, instead of drawing the rectangles. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Parameter is not valid error while drawing a rectangle

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: Parameter is not valid error while drawing a rectangle

Posted 26 June 2011 - 01:30 PM

You might want to check out my tutorial and see if it might help your code.

http://www.dreaminco...bberband-shape/
Was This Post Helpful? 1
  • +
  • -

#3 hyphen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-June 11

Re: Parameter is not valid error while drawing a rectangle

Posted 26 June 2011 - 02:06 PM

Thankyou very much modi123_1, I searched this site for it but I was unaware that this was called the rubberband effect.. Who knew.. That's exactly what I was looking for. Props..
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: Parameter is not valid error while drawing a rectangle

Posted 26 June 2011 - 02:38 PM

No problem. Some days it's all about the "magic search words". Haha.. good luck with the program!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1