手绘绘制矩形 [英] Freehand draw rectangle
问题描述
如果以前的帖子丢失了,这里就是完整的帖子:
我正试图在上面写一个矩形使用鼠标移动图片框图片
事件但问题是矩形选择/绘图不能从左下角到左上角完成
。唯一的选择
我可以做的是从左上角到右下角
方向开始。
代码是:
Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight作为整数
Private Sub p_MouseMove(ByVal sender As Object,ByVal e As
System.Windows.Forms.MouseEventArgs)处理p.MouseMove
如果e.Button = Windows.Forms.MouseButtons.Left那么
''需要计算鼠标开始的位置
' '并计算新的位置
cropWidth = eX - cropX
cropHeight = eY - cropY
''绘制大纲裁剪,p是picbox。
p.CreateGraphics.DrawRectangle(Pens.Aqua,cropX,cropY,cropWidth,
cropHeight)
结束如果
End Sub
我只能从左上角到右下角选择矩形。
不久,选择是不是徒手,固定方向。我怎么能更正这个呢?
谢谢!
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!
推荐答案
kimiraikkonen写道:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua,cropX,cropY,cropWidth,
cropHeight)
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
未经测试,但猜测宽度和高度需要是非负的:
p.CreateGraphics.DrawRectangle(Pens.Aqua,Math.Min (cropX,eX),
Math.Min(cropY,eY),Math.Abs(cropWidth),Math.Abs(cropHeight))
或类似的东西。
Andrew
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
2月6日下午4:58,Andrew Morton < a ... @ in-press.co.uk.invalid>
写道:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen写道:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua,cropX,cropY,cropWidth,
cropHeight)
p.CreateGraphics.DrawRectangle(Pens.Aqua, cropX, cropY, cropWidth,
cropHeight)
未经测试,但猜测宽度和高度需要是非负的:
p.CreateGraphics.DrawRectangle(Pens.Aqua,Math.Min(cropX,eX),
Math.Min(cropY,eY),Math.Abs(cropWidth),Math.Abs(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
安德鲁,
谢谢,现在选择是随心所欲的代码。但是这次如果我想要裁剪这个选择并粘贴到
图片框中的新矩形,我得到了参数无效。此代码出错:
''用于设置源图像的位置和大小的矩形
Dim rect作为新矩形(cropX,cropY,cropWidth ,cropHeight)
Dim bit As Bitmap = New Bitmap(p.Image,p.Width,p.Height)
''创建一个新的位图文本框中指定的宽度/高度值
。
''包含裁剪图像的位图
Dim cropBitmap As Bitmap
cropBitmap =新位图(cropWidth,cropHeight)
''将在cropBitmap上绘制的新Graphics对象
Dim g As Graphics = Graphics.FromImage(cropBitmap)
''绘制你提供裁剪值的图像部分。
g.DrawImage(bit,0 ,0,rect,GraphicsUnit.Pixel)
''" pbcrop"是一个空白的小盒子,一个裁剪的图像将被粘贴。
pbCrop.Image = cropBitmap
我该怎么纠正这个?
>
谢谢!
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!
2月6日下午5:15,kimiraikkonen< kimiraikkone ... @ gmail.comwrote:
On Feb 6, 5:15 pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
2月6日下午4:58,Andrew Morton < a ... @ in-press.co.uk.invalid>
写道:
On Feb 6, 4:58 pm, "Andrew Morton" <a...@in-press.co.uk.invalid>
wrote:
kimiraikkonen写道:
kimiraikkonen wrote:
p.CreateGraphics.DrawRectangle(Pens.Aqua,cropX,cropY,cropWidth,
cropHeight)
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,eX),
Math.Min(cropY,eY),Math.Abs(cropWidth),Math.Abs(cropHeight))
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
Andrew,
谢谢,现在选择是与您的代码徒手。但是这次如果我想要裁剪这个选择并粘贴到
图片框中的新矩形,我得到了参数无效。此代码出错:
''用于设置源图像的位置和大小的矩形
Dim rect作为新矩形(cropX,cropY,cropWidth ,cropHeight)
Dim bit As Bitmap = New Bitmap(p.Image,p.Width,p.Height)
''创建一个新的位图文本框中指定的宽度/高度值
。
''包含裁剪图像的位图
Dim cropBitmap As Bitmap
cropBitmap =新位图(cropWidth,cropHeight)
''将在cropBitmap上绘制的新Graphics对象
Dim g As Graphics = Graphics.FromImage(cropBitmap)
''绘制你提供裁剪值的图像部分。
g.DrawImage(bit,0 ,0,rect,GraphicsUnit.Pixel)
''" pbcrop"是一个空白的小盒子,一个裁剪的图像将被粘贴。
pbCrop.Image = cropBitmap
我该怎么纠正这个?
>
谢谢!
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!
注意:代码因参数无效而失败关于此代码:
cropBitmap =新位图(cropWidth,cropHeight)
更改为:
cropBitmap =(Math.Abs(cropWidth),Math.Abs(cropHeight))消除了
错误,但没有看到裁剪的图像。
是有什么必须做的另外吗?
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?
这篇关于手绘绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!