473,382 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 5442
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
1
by: Praveen | last post by:
Hello, I have a web page which will display the map of a city/place. I have a toolbar which contains 'zoom in' and 'zoom out' buttons. When the user clicks on zoom in/zoom out button, he should...
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
1
by: Rob Richardson | last post by:
Greetings! I am creating a form that will contain information that will eventually be on a label. The label has a 2-column table with lines separating the cells. I want my form to resemble the...
3
by: Tom | last post by:
Hi Hi i am trying to draw on top of a button on a standard toolbar. All i want to do is draw a small rectangle on the button to represent the selected color. I have tried the folowing code in...
7
by: Mark Ingram | last post by:
Hi, how can i draw a rounded rectange, with a border within a specified area? i.e. if i have a Rectangle with width and height of 100, how can i draw a rectange with 2 pixel border inside of the...
1
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw...
11
by: dongarbage | last post by:
Hi there, I'm very much a C# novice. How do you do freehand drawing on a panel with a mouse in c#? Thanks, Don
1
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.