VB 缩小坐标

VB Downscaling coordinates

我需要知道鼠标点击 gdi+ 画线的时间。

我设计了这个函数,它在所有现有线路上循环使用,函数的作用是:

  1. 它为该行的容器大小创建一个缓冲区
  2. 它使整个东西变黑
  3. 它以绿色画线
  4. 获取鼠标所在位置的像素
  5. 如果像素不同于黑色 a.k.a 绿色,则线已被成功点击,函数应 return 为真。

这很好用,没有误解,但我担心当我的表单全屏显示时(由于缓冲区较大)会有微小的延迟(不是很明显)。

我正在寻找一种优化方法,我的第一个想法是缩小所有内容。所以我的意思是让缓冲区像 20x20 一样,然后使用数学在缩小版本中画线。问题是,我的数学很烂,所以我基本上是在问你如何做到这一点,最好是对傻瓜进行解释。

这是函数:

Public Function Contains(ByVal e As Point) As Boolean
    Dim Width As Integer = Container.Size.Width
    Dim Height As Integer = Container.Size.Height

    Dim Buffer As Bitmap = New Bitmap(Width, Height)
    Using G As Graphics = Graphics.FromImage(Buffer)
        G.Clear(Color.Black)

        Dim Start As Point = New Point(ParentNode.Location.X + ParentNode.Size.Width / 2, ParentNode.Location.Y + ParentNode.Size.Height / 2)
        Dim [End] As Point = New Point(ChildNode.Location.X + ChildNode.Size.Width / 2, ChildNode.Location.Y + ChildNode.Size.Height / 2)

        Dim Control1 As Point
        Dim Control2 As Point
        Control1.X = Start.X + GetAngle(ChildNode.Location, ParentNode.Location, ChildNode.Location.X - ParentNode.Location.X, ChildNode.Location.Y - ParentNode.Location.Y)
        Control1.Y = Start.Y

        Control2.X = [End].X
        Control2.Y = Start.Y

        G.DrawBezier(New Pen(Color.Green, 4), Start, Control1, Control2, [End])
    End Using

    If Buffer.GetPixel(e.X, e.Y).ToArgb() <> Color.Black.ToArgb() Then
        Return True
    End If

    Return False
End Function

这是我使函数使用上述想法的尝试之一:

Public Function Contains(ByVal e As Point) As Boolean
    Dim Width As Integer = 20
    Dim Height As Integer = 20

    Dim Buffer As Bitmap = New Bitmap(Width, Height)
    Using G As Graphics = Graphics.FromImage(Buffer)
    G.Clear(Color.Black)

    Dim Start As Point = New Point(ParentNode.Location.X + ParentNode.Size.Width / 2, ParentNode.Location.Y + ParentNode.Size.Height / 2)
    Dim [End] As Point = New Point(ChildNode.Location.X + ChildNode.Size.Width / 2, ChildNode.Location.Y + ChildNode.Size.Height / 2)

    Dim Control1 As Point
    Dim Control2 As Point
    Control1.X = Start.X + GetAngle(ChildNode.Location, ParentNode.Location, ChildNode.Location.X - ParentNode.Location.X, ChildNode.Location.Y - ParentNode.Location.Y)
    Control1.Y = Start.Y

    Control2.X = [End].X
    Control2.Y = Start.Y

    G.DrawBezier(New Pen(Color.Green, 4), New Point(Start.X / Width, Start.Y / Height), New Point(Control1.X / Width, Control1.Height / Height), New Point(Control2.X / Width, Control2.Y / Height), New Point([End].X / Width, [End].Y / Height))
End Using

If Buffer.GetPixel(Width, Height).ToArgb() <> Color.Black.ToArgb() Then
    Return True
End If

Return False
End Function

尝试使用 GraphicsPath for drawing and testing with the built-in IsOutlineVisible 函数:

Public Function Contains(ByVal e As Point) As Boolean
  Dim result as Boolean = False
  Using gp As New GraphicsPath
    gp.AddBezier(your four points)
    Using p As New Pen(Color.Empty, 4)
      result = gp.IsOutlineVisible(e, p)
    End Using
  End Using
  Return result
End Function

旁注:位图和图形对象需要在创建时进行处理。