title: Calling GetPixel from powershell with param error despite correct params
body: <p>The code snippet below is the debugger output.</p>
  
  <pre><code>Exception calling "GetPixel" with "2" argument(s): "Parameter must be positive and &lt; Height.
  Parameter name: y"
  At C:\dropbox\Workspace\PowerShellTest\mapGenerator\mapGenerator.ps1:51 char:16
  +             if(($map.GetPixel($x, ($y+1)) -eq "ff000000") -or ($map.GetPixel(($x ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ArgumentOutOfRangeException
  
  Hit Line breakpoint on 'C:\dropbox\Workspace\PowerShellTest\mapGenerator\mapGenerator.ps1:51'
  
  
  [DBG]: PS C:\dropbox\Workspace\PowerShellTest\mapGenerator&gt;&gt; $y
  5
  
  [DBG]: PS C:\dropbox\Workspace\PowerShellTest\mapGenerator&gt;&gt; $map.Height
  7
  </code></pre>
  
  <p>I do not get this error message at all.</p>
  
  <p>"Parameter must be positive and &lt; Height."</p>
  
  <p>Ok. But parameter is (5+1), so positive, and (5+1), so &lt;7</p>
  
  <p><strong>MCVE Code</strong></p>
  
  <p>EDIT 1:
  MCVE code removed - I found an error in the MCVE which, when removed, also got rid of the error message. Now to go back and see if I have the same error in the original code.</p>
  
  <p>EDIT 2: This is infuriating. I inserted a write-output for the $x and $y values, just to make sure I didn't have any with bad values (E.g. checking below a pixel with coordinate 0, or above a pixel with coordinate map.height <em>Inserting the write-output made the rest of the code run correctly.</em> And removing the write-output makes the code stop working again. Does write-output coerce something behind the scenes?</p>
  
  <p>EDIT 3: It is very difficult to make an MCVE that retains this weird behavior, so I'll give you the script it happens in.</p>
  
  <pre><code>function markCoastline ($landArray, $map)
  {
         foreach ($square in $landArray)
      {
  
          $x = $square[0]
          $y = $square[1]
  
          $top = ($y -eq $map.Height-1)
          $bot = ($y -eq 0)
          $lef = ($x -eq 0)
          $rig = ($x -eq $map.Width-1)
  
  
  
          Write-Output $x
          Write-Output $y
  
  
          if(     $bot -and $left)
          {
              if(($map.GetPixel($x, ($y+1)).Name -eq "ff000000")-or ($map.GetPixel(($x+1), $y).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          elseif( $top -and $left)
          {
              if(($map.GetPixel(($x+1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y-1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          elseif( $bot -and $rig)
          {
              if(($map.GetPixel(($x-1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y+1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          elseif( $top -and $rig)
          {
              if(($map.GetPixel(($x-1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y-1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          elseif(  $lef)
          {
              if(($map.GetPixel($x, ($y+1)).Name -eq "ff000000") -or ($map.GetPixel(($x+1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y-1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          elseif(  $bot)
          {
              if(($map.GetPixel($x, ($y+1)).Name -eq "ff000000") -or ($map.GetPixel(($x+1), $y).Name -eq "ff000000") -or ($map.GetPixel(($x-1), $y).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }            
          }
          elseif(  $rig)
          {
              if(($map.GetPixel($x, ($y+1)).Name -eq "ff000000")-or ($map.GetPixel(($x-1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y-1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          elseif(  $top)
          {
              if(($map.GetPixel(($x+1), $y).Name -eq "ff000000") -or ($map.GetPixel(($x-1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y-1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
          else
          {
              if(($map.GetPixel($x, ($y+1)).Name -eq "ff000000") -or ($map.GetPixel(($x+1), $y).Name -eq "ff000000") -or ($map.GetPixel(($x-1), $y).Name -eq "ff000000") -or ($map.GetPixel($x, ($y-1)).Name -eq "ff000000"))
              {
                  $map.SetPixel($x, $y, "lime")
              }
          }
      }
  }
  
  #First, get a BMP image
  $openFile = New-Object -TypeName System.Windows.Forms.OpenFileDialog
      $openFile.AddExtension = $true
      $openFile.Filter = 'Bitmap Picture (*.bmp)|*.bmp|All Files|*.*'
      $openFile.Multiselect = $false
      $openFile.FilterIndex = 0
      $openFile.InitialDirectory = "$HOME\Documents"
      $openFile.RestoreDirectory = $true
      $openFile.ShowReadOnly = $true
      $openFile.ReadOnlyChecked = $false
      $openFile.Title = 'Select a seed picture (BMP only)'
  $openFile.showDialog()
  $map = New-Object System.Drawing.Bitmap($openFile.Filename)
  
  $landPixels = @()
  $oceanPixels = @()
  
  
  #Second: Find land/ocean
  for ($x = 0; $x -lt $map.Width; $x++)
  {
      for ($y = 0; $y -lt $map.Height; $y++)
      {
          $current = $map.GetPixel($x, $y)
  
          if($current.Name -eq "ff000000")
          {
              $ocean = @($x, $y)
              $oceanPixels += , $ocean
          }
          else
          {
              $land = @($x, $y)
              $landPixels += , $land
          }
      }
  }
  
  
  #Third, paint coastline green:
  
  markCoastline $landPixels $map
  
  $map.save("b8fcf4351caf4b00af8eb0fa2e5bc17a.bmp")
  </code></pre>
  
  <p>There's a write-output on lines 16 and 17. Remove those and the script gives the original problem mentioned above - claims $y doesn't fit as a parameter for the GetPixel function. Keep it in and the code runs exactly as intended.</p>