在图片框中绘制rect而不是为鼠标右对齐

时间:2014-10-20 17:24:24

标签: vb.net winforms picturebox rect

我目前有一个图片框,用户可以在其中单击并拖动以在图像上绘制一个矩形(可以定期更改的图像)。当它们完成时(mouse_up),我将在文本框中显示rect的相对点到分辨率。

因此,例如,用户从一个1920 x 680图像(picturebox.right,picturebox.bottom)的左上角(0,0)到右下角绘制一个rect,文本框将显示(1920,680) )为终点。这主要是比例的东西。

我使用上一个问题(Having trouble drawing simple rectangle in picturebox)的答案中的代码来绘制它。

问题:该框不会跟随鼠标,因为图像必须在拉伸模式下完成。它们通常很大(如1920 x 680),并且不适合常规的gui。有多种分辨率,因此必须采用动态比率。如果不进行编辑,此代码在正常模式下运行良好,但这对可用性不起作用。因此,当您绘制框时,它非常小而且与鼠标无关(因此我无法在文本框中显示结束点)。

这是我的意思的一个例子。我把鼠标拖到图像的中间: enter image description here

我尝试了什么:我试图通过比率来对抗它,但它仍然无法解决显示终点问题,或者它是否真的跟着鼠标那么好。它通常偏离左侧至少10个左右的像素。这是我调整后的代码:

    Private Sub DrawRectangle(ByVal pnt As Point)

    Try
        Dim g As Graphics

        g = Graphics.FromImage(img)

        g.DrawImage(imgClone, 0, 0) 'we are clearing img with imgClone. imgClone contains the original image without the rectangles

        Dim w_ratio As Integer = Math.Floor(img.Width / pbZoneImage.Width)
        Dim h_ratio As Integer = Math.Floor(img.Height / pbZoneImage.Height)
        Dim customPen As New Pen(currentColor, 5)

        'If pnt.X = mouse_Down.X Or pnt.Y = mouse_Down.Y Then
        '    g.DrawLine(customPen, mouse_Down.X, mouse_Down.Y, pnt.X * w_ratio, pnt.Y * h_ratio)

        'Else
        theRectangle = New Rectangle(Math.Min(mouse_Down.X, pnt.X * w_ratio), Math.Min(mouse_Down.Y, pnt.Y * h_ratio),
                    Math.Abs(mouse_Down.X - pnt.X * w_ratio), Math.Abs(mouse_Down.Y - pnt.Y * h_ratio))

        g.DrawRectangle(customPen, theRectangle)

        'End If

        g.Dispose()

        pbZoneImage.Invalidate() 'draw img to picturebox


    Catch ex As Exception

    End Try

End Sub

我也尝试过让结束显示点(x,y)与矩形的相对端匹配,但同样不能使用比率。

关于如何使这项工作与普通模式中的工作一样的想法和拉伸时的想法一样?我也对不同的控件或一般的任何提示持开放态度。谢谢!

1 个答案:

答案 0 :(得分:1)

这可以通过多种方式完成,但最简单的方法是使用带有 SizeMode = Normal 图片框。加载图片:

img = New Bitmap(pbZoneImage.Width, pbZoneImage.Height)

imgClone = My.Resources.... 'real dimensions

Dim g As Graphics = Graphics.FromImage(img)

'it will scale the image, no need for stretch mode
g.DrawImage(imgClone, 0, 0, pbZoneImage.Width, pbZoneImage.Height)

g.Dispose()

pbZoneImage.Image = img

然后正常画画:

Private Sub DrawRectangle(ByVal pnt As Point)

Try
    Dim g As Graphics

    g = Graphics.FromImage(img)

    g.DrawImage(imgClone, 0, 0, pbZoneImage.Width, pbZoneImage.Height) 'we are clearing img with imgClone. imgClone contains the original image without the rectangles

    Dim customPen As New Pen(currentColor, 5)

    'If pnt.X = mouse_Down.X Or pnt.Y = mouse_Down.Y Then
    '    g.DrawLine(customPen, mouse_Down.X, mouse_Down.Y, pnt.X * w_ratio, pnt.Y * h_ratio)

    'Else
    theRectangle = New Rectangle(Math.Min(mouse_Down.X, pnt.X), Math.Min(mouse_Down.Y, pnt.Y),
                Math.Abs(mouse_Down.X - pnt.X), Math.Abs(mouse_Down.Y - pnt.Y))

    g.DrawRectangle(customPen, theRectangle)

    'End If

    g.Dispose()

    pbZoneImage.Invalidate() 'draw img to picturebox


    Catch ex As Exception

    End Try

End Sub

mouse up event比例获得正确的结果:

Private Sub pbZoneImage_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles pbZoneImage.MouseUp
    Dim width, height As Integer

    width = CInt(Math.Abs(mouse_Down.X - e.X) * (imgClone.Width / pbZoneImage.Width))
    height = CInt(Math.Abs(mouse_Down.Y - e.Y) * (imgClone.Height / pbZoneImage.Height))

    TextBox1.Text = width.ToString + " " + height.ToString
End Sub