473,599 Members | 3,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByV al sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles p.MouseMove

If e.Button = Windows.Forms.M ouseButtons.Lef t 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.CreateGraphic s.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 5459
kimiraikkonen wrote:
p.CreateGraphic s.DrawRectangle (Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:

p.CreateGraphic s.DrawRectangle (Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWi dth), Math.Abs(cropHe ight))

Or something like that.

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

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

p.CreateGraphic s.DrawRectangle (Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWi dth), Math.Abs(cropHe ight))

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(cropWidt h, cropHeight)

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

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

' "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.comwrot e:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.inv alid>
wrote:
kimiraikkonen wrote:
p.CreateGraphic s.DrawRectangle (Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:
p.CreateGraphic s.DrawRectangle (Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWi dth), Math.Abs(cropHe ight))
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(cropWidt h, cropHeight)

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

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

' "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(cropWidt h, cropHeight)

Changing it to:

cropBitmap = (Math.Abs(cropW idth), Math.Abs(cropHe ight)) 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.inv alid>
wrote:
>kimiraikkone n wrote:
>>p.CreateGraph ics.DrawRectang le(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)

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

p.CreateGraphi cs.DrawRectangl e(Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY , e.Y), Math.Abs(cropWi dth), Math.Abs(cropHe ight))

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.inv alid>
wrote:
kimiraikkonen wrote:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.inv alid>
wrote:
kimiraikkonen wrote:
p.CreateGraphi cs.DrawRectangl e(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
Untested, but guessing that width and height need to be non-negative:
p.CreateGraphic s.DrawRectangle (Pens.Aqua, Math.Min(cropX, e.X),
Math.Min(cropY, e.Y), Math.Abs(cropWi dth), Math.Abs(cropHe ight))
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.CreateGraphi cs.DrawRectangl e(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"
>>kimiraikkon en 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+cro pwidth
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.inv alid>
wrote:
Andrew Morton wrote:
kimiraikkonen wrote:
p.CreateGraphic s.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.inv alid>
wrote:
kimiraikkonen wrote:
On Feb 6, 5:43 pm, "Andrew Morton"
>kimiraikkone n 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+cro pwidth
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4237
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
1
2575
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 be able to draw a rectangle on any part of the map. The map will refresh automatically with the zoom selections. Can any one help me on how to draw the rectangle (without any predefined co-ords) and capture the starting and ending points of the...
3
10317
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 shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
1
3417
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 label, so I want to draw a rectangle and lines on my form. I don't want them to have any user interaction whatever. They would be there just to look pretty. There's plenty of support for drawing rectangles from code. There seems to be support...
3
1637
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 the toolbar's parent control, which is fired when the selected color is changed: Dim r As Rectangle = FontColorButton.Rectangle
7
10684
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 original one? (the current attempts all draw the border just outside of the original rectangle). At the moment im using the following code, but it allows the rectangle to grow - which i definately dont want!
1
3756
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 the rectangle using the right mouse button to represent the area of the chart they want to zoom on. I haev been able to draw the rectangle on a blank form but I cannot get it to draw on top of other controls. I have pasted in the code i am using ....
11
11257
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
5114
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 following code. in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous...
0
7992
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8398
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8400
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8267
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3940
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2414
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.