Freehand draw rectangle

K

kimiraikkonen

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!
 
A

Andrew Morton

kimiraikkonen said:
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
 
K

kimiraikkonen

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!
 
K

kimiraikkonen

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?
 
A

Andrew Morton

kimiraikkonen said:
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
 
K

kimiraikkonen

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.
 
A

Andrew Morton

Andrew said:
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/[email protected]/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
 
A

Andrew Morton

kimiraikkonen said:
On Feb 6, 5:43 pm, "Andrew Morton"

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
 
K

kimiraikkonen

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/[email protected]/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 :-(
 
K

kimiraikkonen

(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.
 
A

Andrew Morton

kimiraikkonen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top