I do not know why my nested for next loop is only going through one loop

孤者浪人 提交于 2019-12-13 03:41:57

问题


I am currently making a maze game on Visual Basic and I need to retrieve the colours for all of the pixels in the image. To do this I've made a nested for next loop - 1 for width and one for height, as the code iterates through both of the loops it will get the pixel colors of each pixel and place it inside a 2 dimensional array.

The issue is it is only iterating through the length and not the width

Here my code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim widthIMG As Integer
    Dim lengthIMG As Integer
    Dim pixels(438, 343) As Color
    Dim pixelsData(438, 343) As String
    Dim myBitmap As New Drawing.Bitmap(PictureBox1.Image)


    For widthIMG = 1 To myBitmap.Width
        For lengthIMG = 1 To myBitmap.Height

            pixels(widthIMG, lengthIMG) = myBitmap.GetPixel(widthIMG, lengthIMG)

            Select Case pixels(widthIMG, lengthIMG).ToString
                Case "Color [A=255, R=0, G=0, B=0]"
                    TextBox1.Text = TextBox1.Text & "Width: " & widthIMG & "Length: " & lengthIMG & "Color: " & "Black " & vbCrLf
                    pixelsData(widthIMG, lengthIMG) = "Black"
                Case "Color [A=255, R=255, G=255, B=255]"

                    TextBox1.Text = TextBox1.Text & "Width: " & widthIMG & "Length: " & lengthIMG & "Color: " & "White" & vbCrLf
                    pixelsData(widthIMG, lengthIMG) = "White"

            End Select
        Next
    Next
End Sub

回答1:


You're out of bounds. Try again with this modification:

For widthIMG = 0 To myBitmap.Width - 1
    For lengthIMG = 0 To myBitmap.Height - 1

The reason behind this is that arrays starts at zero. When you do:

For index As Integer = 1 To 5
    Debug.Write(index.ToString & ", ")
Next

Your output will be 1, 2, 3, 4, 5.

But the array of your image starts at zero, and ends one integer less than it's shown size. So an array of, let's say 342, is really 0 to 341.




回答2:


Your problem here is probably because there's an exception that exit your program. Image coordinate starts at 0. Right now, when lengthIMG is equal to myBitmap.Height, you would get an error.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim x As Integer
    Dim y As Integer
    Dim pixels(438, 343) As Color
    Dim pixelsData(438, 343) As String
    Dim myBitmap As New Drawing.Bitmap(PictureBox1.Image)


    For x = 0 To myBitmap.Width - 1
        For y = 0 To myBitmap.Height - 1

            pixels(x, y) = myBitmap.GetPixel(x, y)

            Select Case pixels(x, y).ToString
                Case "Color [A=255, R=0, G=0, B=0]"
                    TextBox1.Text = TextBox1.Text & "X: " & x & " Y: " & y & " Color: " & "Black " & vbCrLf
                    pixelsData(x, y) = "Black"
                Case "Color [A=255, R=255, G=255, B=255]"

                    TextBox1.Text = TextBox1.Text & "X: " & x & " Y: " & y & " Color: " & "White" & vbCrLf
                    pixelsData(x, y) = "White"

            End Select
        Next
    Next
End Sub

This assume that your image isn't bigger than 438, 343 because your array is fixed.

Also, please use x, y for the coordinate, that is the standard :)

One last tip, I would separate your logic into smaller method. One method to load the bitmap and return a pixelsData. One method that takes a pixelsData and return a string for the textbox. That way, you could easily output to a file instead of the textbox.



来源:https://stackoverflow.com/questions/53740313/i-do-not-know-why-my-nested-for-next-loop-is-only-going-through-one-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!