469,909 Members | 1,870 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

Freehand draw rectangle

Hi,
If previous post was missing, here's the complete one:

I'm trying to draw a rectange on a picturebox image using mouse move
event but the problem is that the rectangle selection / drawing cannot
be done from starting from bottom-right to up-left. The only selection
i'm allowed to do is starting from top-left towards bottom-right
orientation.

The code is:
Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight As Integer

Private Sub p_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles p.MouseMove

If e.Button = Windows.Forms.MouseButtons.Left Then
' need to take into count where the mouse started at
'and calculate the new position
cropWidth = e.X - cropX
cropHeight = e.Y - cropY

'draw the outline for the cropping, "p" is picbox.
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
End If

End Sub

I can only select rectangle from top-left towards bottom-righ.
Shortly, the selection is NOT freehand, fixed direction. How can i
correct this?

Thanks!
Feb 6 '08 #1
10 5183
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:

p.CreateGraphics.DrawRectangle(Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWidth), Math.Abs(cropHeight))

Or something like that.

Andrew
Feb 6 '08 #2
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)

Untested, but guessing that width and height need to be non-negative:

p.CreateGraphics.DrawRectangle(Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWidth), Math.Abs(cropHeight))

Or something like that.

Andrew
Andrew,
Thanks, now the selection is freehand with your code. But this time if
i want to crop this selection and paste to as a new rectangle in a
picturebox, i got the "parameter is not valid" error with this code:

'a rectangle to set the location and size from the source image
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)

Dim bit As Bitmap = New Bitmap(p.Image, p.Width, p.Height)
'create a new bitmap with the width/height values that were specified
in the textboxes.
'bitmap to contain the cropped image
Dim cropBitmap As Bitmap
cropBitmap = New Bitmap(cropWidth, cropHeight)

'a new Graphics object that will draw on the cropBitmap
Dim g As Graphics = Graphics.FromImage(cropBitmap)

'draw the portion of the image that you supplied cropping values for.
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)

' "pbcrop" is blank picbox that a cropped image will be pasted into.
pbCrop.Image = cropBitmap

How can i correct this?

Thanks!
Feb 6 '08 #3
On Feb 6, 5:15 pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:
p.CreateGraphics.DrawRectangle(Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWidth), Math.Abs(cropHeight))
Or something like that.
Andrew

Andrew,
Thanks, now the selection is freehand with your code. But this time if
i want to crop this selection and paste to as a new rectangle in a
picturebox, i got the "parameter is not valid" error with this code:

'a rectangle to set the location and size from the source image
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)

Dim bit As Bitmap = New Bitmap(p.Image, p.Width, p.Height)
'create a new bitmap with the width/height values that were specified
in the textboxes.
'bitmap to contain the cropped image
Dim cropBitmap As Bitmap
cropBitmap = New Bitmap(cropWidth, cropHeight)

'a new Graphics object that will draw on the cropBitmap
Dim g As Graphics = Graphics.FromImage(cropBitmap)

'draw the portion of the image that you supplied cropping values for.
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)

' "pbcrop" is blank picbox that a cropped image will be pasted into.
pbCrop.Image = cropBitmap

How can i correct this?

Thanks!
Note: The code fails as "parameter is not valid" on this code:

cropBitmap = New Bitmap(cropWidth, cropHeight)

Changing it to:

cropBitmap = (Math.Abs(cropWidth), Math.Abs(cropHeight)) eliminated
error but no cropped image is visible.

Is there anything that must be done additionaly?
Feb 6 '08 #4
kimiraikkonen wrote:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
>kimiraikkonen wrote:
>>p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)

Untested, but guessing that width and height need to be non-negative:

p.CreateGraphics.DrawRectangle(Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWidth), Math.Abs(cropHeight))

Or something like that.

Andrew

Andrew,
Thanks, now the selection is freehand with your code. But this time if
i want to crop this selection and paste to as a new rectangle in a
picturebox, i got the "parameter is not valid" error with this code:

'a rectangle to set the location and size from the source image
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
I'm guessing that you'll need to get the coordinates such that cropWidth and
cropHeight are non-negative, in the way I did in my previous post.

Andrew
Feb 6 '08 #5
On Feb 6, 5:43 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen wrote:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:
p.CreateGraphics.DrawRectangle(Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWidth), Math.Abs(cropHeight))
Or something like that.
Andrew
Andrew,
Thanks, now the selection is freehand with your code. But this time if
i want to crop this selection and paste to as a new rectangle in a
picturebox, i got the "parameter is not valid" error with this code:
'a rectangle to set the location and size from the source image
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)

I'm guessing that you'll need to get the coordinates such that cropWidth and
cropHeight are non-negative, in the way I did in my previous post.

Andrew
Thanks but which code line refers to it?
I've tried your code and did well for selecting freehand, the problem
is cropping the selection resulting with "parameter is not valid"
error described on code line of my previous post.
Feb 6 '08 #6
Andrew Morton wrote:
kimiraikkonen wrote:
>p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)

Untested, but guessing that width and height need to be non-negative:
I have now tested it under .NET 3.5.

It is apparently a bug in the .NET framework (unless it's in the underlying
GDI+, and/or there is some rationale behind it), as DrawLine and DrawEllipse
both draw the expected shapes with negative width and height values.

They've copied the behaviour in Mono:
http://www.mail-archive.com/mo******.../msg11968.html

I suppose that once a bug like that has been incorporated, they can't mend
it in case it breaks someone's bug-fix.

Although I can't help but wonder why I can't find many references to the
problem on google or google groups.

Andrew
Feb 6 '08 #7
kimiraikkonen wrote:
On Feb 6, 5:43 pm, "Andrew Morton"
>>kimiraikkonen wrote:
'a rectangle to set the location and size from the source image
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)

I'm guessing that you'll need to get the coordinates such that
cropWidth and cropHeight are non-negative, in the way I did in my
previous post.

Andrew

Thanks but which code line refers to it?
I've tried your code and did well for selecting freehand, the problem
is cropping the selection resulting with "parameter is not valid"
error described on code line of my previous post.
(Completely untested)

if cropWidth<0 then
cropx=cropx+cropwidth
cropwidth= -cropwidth
end if
<type the same code but for Y>
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)

Andrew
Feb 6 '08 #8
On Feb 6, 6:02 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
Andrew Morton wrote:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:

I have now tested it under .NET 3.5.

It is apparently a bug in the .NET framework (unless it's in the underlying
GDI+, and/or there is some rationale behind it), as DrawLine and DrawEllipse
both draw the expected shapes with negative width and height values.

They've copied the behaviour in Mono:
http://www.mail-archive.com/mono-b...../msg11968.html

I suppose that once a bug like that has been incorporated, they can't mend
it in case it breaks someone's bug-fix.

Although I can't help but wonder why I can't find many references to the
problem on google or google groups.

Andrew
Wow, new bug has been found accidently? Since the trial of code and
still, i'm supposing there would be a coding mistake may have caused
this :-(
Feb 6 '08 #9
On Feb 6, 6:06 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen wrote:
On Feb 6, 5:43 pm, "Andrew Morton"
>kimiraikkonen wrote:
'a rectangle to set the location and size from the source image
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
I'm guessing that you'll need to get the coordinates such that
cropWidth and cropHeight are non-negative, in the way I did in my
previous post.
Andrew
Thanks but which code line refers to it?
I've tried your code and did well for selecting freehand, the problem
is cropping the selection resulting with "parameter is not valid"
error described on code line of my previous post.

(Completely untested)

if cropWidth<0 then
cropx=cropx+cropwidth
cropwidth= -cropwidth
end if
<type the same code but for Y>
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)

Andrew
Andrew, very thanks the last post of you did the trick! GDI is a bit
complicated really :-)

Thank you so much.
Feb 6 '08 #10
kimiraikkonen wrote:
Andrew, very thanks the last post of you did the trick! GDI is a bit
complicated really :-)

Thank you so much.
You're welcome :-)

Andrew
Feb 6 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Praveen | last post: by
1 post views Thread by Rob Richardson | last post: by
3 posts views Thread by Tom | last post: by
7 posts views Thread by Mark Ingram | last post: by
11 posts views Thread by dongarbage | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.