【问题标题】:Is there a way to convert Pixel coordinates to Cartesian Coordinates in VB.net有没有办法在 VB.net 中将像素坐标转换为笛卡尔坐标
【发布时间】:2019-01-08 14:52:14
【问题描述】:

我有一个大小为 1096 x 1004 的 PictureBox,SizeMode 设置为 StretchImage。通过考虑像素坐标上的 StrechImage 效果,我能够正确获取每个像素的坐标(参见下面的代码)。

现在我想要完成的是将这些像素坐标转换为笛卡尔坐标以便能够绘制图形。从长远来看,我将采用笛卡尔坐标并将其转换为极坐标。

我曾尝试使用此方法将像素坐标转换为笛卡尔坐标。

cartesianx = scalefactor*screenx - screenwidth / 2;
cartesiany = -scalefactor*screeny + screenheight / 2;

此方法不会将原点 (0,0) 放在 PictureBox 的中心。它似乎将原点设置为更靠近 PictureBox 的左上角。关于我缺少什么有什么想法吗?

下面是我将图像转换为位图并获取这些坐标并正确缩放它们的代码。

Imports System.IO

Public Class HomePanel

Dim realX As Int32
Dim realY As Int32

Private Sub HomePanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    chartImageDisplay_box.Image = Image.FromFile("C:\Users\UserB\Desktop\test.jpg")

End Sub


Private Sub chartImageDisplay_box_MouseMove(sender As Object, e As MouseEventArgs) Handles chartImageDisplay_box.MouseMove

    If (e.Button = MouseButtons.Left) Then
        ShowCoords(e.X, e.Y)
    End If

End Sub



Private Sub chartImageDisplay_box_MouseDown(sender As Object, e As MouseEventArgs) Handles chartImageDisplay_box.MouseDown
    If (e.Button = MouseButtons.Left) Then
        ShowCoords(e.X, e.Y)

        Dim MyBitmap As Bitmap
        MyBitmap = CType(chartImageDisplay_box.Image, Bitmap)
        'Me.BackColor = MyBitmap.GetPixel(realX, realY)
        rgbValue.Text = "RGB Value: " & MyBitmap.GetPixel(realX, realY).ToString()

    End If
    'printAllPixels()

End Sub


Private Sub ShowCoords(ByVal mouseX As Int32, ByVal mouseY As Int32)
    Dim realW As Int32 = chartImageDisplay_box.Image.Width
    Dim realH As Int32 = chartImageDisplay_box.Image.Height
    Dim currentW As Int32 = chartImageDisplay_box.ClientRectangle.Width
    Dim currentH As Int32 = chartImageDisplay_box.ClientRectangle.Height
    Dim zoomW As Double = (currentW / CType(realW, Double))
    Dim zoomH As Double = (currentH / CType(realH, Double))
    Dim zoomActual As Double = Math.Min(zoomW, zoomH)
    Dim padX As Double = If(zoomActual = zoomW, 0, (currentW - (zoomActual * realW)) / 2)
    Dim padY As Double = If(zoomActual = zoomH, 0, (currentH - (zoomActual * realH)) / 2)
    realX = CType(((mouseX - padX) / zoomActual), Int32)
    realY = CType(((mouseY - padY) / zoomActual), Int32)
    lblPosXval.Text = "X: " & If(realX < 0 OrElse realX > realW, "-", realX.ToString())
    lblPosYVal.Text = "Y: " & If(realY < 0 OrElse realY > realH, "-", realY.ToString())
    cartX.Text = "X: " 'Where to add the cart conversion for X
    cartY.Text = "Y: " 'Where to add the cart conversion for Y

End Sub


'Writes all the pixels to a text file along with RGB values for each pixel
Public Sub printAllPixels()
    Using writer As StreamWriter =
        New StreamWriter("C:\Users\UserB\Desktop\Pixels.txt")

        Dim MyBitmap As Bitmap
        MyBitmap = CType(chartImageDisplay_box.Image, Bitmap)


        For y = 0 To MyBitmap.Height - 1
            For x = 0 To MyBitmap.Width - 1
                writer.WriteLine("XY Coord: " & x & ", " & y & ";   " & MyBitmap.GetPixel(x, y).ToString)
            Next
        Next


    End Using
End Sub
End Class

【问题讨论】:

    标签: vb.net pixel


    【解决方案1】:

    我不知道变量的内容是否包含正确的值,但公式应该更像这样:

    cartesianx = scalefactor * (screenx - (screenwidth / 2))
    cartesiany = -scalefactor* (screeny - (screenheight / 2))
    

    转换为 0,0 添加比例因子,然后翻转 y。

    【讨论】:

    • 嘿,莲花,恭喜10k!欢迎来到俱乐部! :)
    【解决方案2】:

    我相信我已经弄清楚了我的问题。我为我的 screenx 和 screeny 使用了错误的值。我使用的是计算出的比例值,但我只需要使用鼠标事件 X 和 Y 值。

    【讨论】:

      猜你喜欢
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多