Argument out of range exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pcnerd

    Argument out of range exception

    I'm trying to create a program that plots randomly colored pixels on a bitmap
    & then displays the bitmap. When I run the program, I see the pixels being
    plotted down the left side of the form. When it gets to the bottom, the
    "Argument out of range exception" occurs. So, obviously the program starts.
    The Help tells me that the argument isn't within the range of values, but it
    doesn't explain how to fix the problem. So the Help doesn't help! It doesn't
    tell me what the range of values are. What are the range of values? The
    problem is with the "Y" parameter. The debugger tells me that the value
    cannot be negative & must be less than Height.

    Here follows the source code:

    Private Sub PointillismTool StripMenuItem_C lick(ByVal sender As
    System.Object, ByVal e As System.EventArg s) Handles
    PointillismTool StripMenuItem.C lick

    Dim objGraphics As Graphics
    Dim objRandom As System.Random
    Dim RedColor As Integer
    Dim GreenColor As Integer
    Dim BlueColor As Integer
    Dim Transparency As Integer

    ' Create a Graphics object using the memory bitmap.
    objGraphics = Graphics.FromIm age(objDrawingS urface)

    ' Initialize the Random object.
    objRandom = New Random(Now.Mill isecond)

    Transparency = objRandom.Next( 0, 256)

    Dim Xcount As Integer
    Dim Ycount As Integer

    For Xcount = 1 To 1280
    For Ycount = 1 To 964
    RedColor = objRandom.Next( 0, 256)
    GreenColor = objRandom.Next( 0, 256)
    BlueColor = objRandom.Next( 0, 256)
    objDrawingSurfa ce.SetPixel(Xco unt, Ycount, Color.FromArgb( RedColor,
    GreenColor, BlueColor))
    ' Draw myBitmap to the screen.
    Me.CreateGraphi cs.DrawImage(ob jDrawingSurface , 0, 0)
    Next Ycount
    Next Xcount

    End Sub

    The problem occurs within the "For Ycount" loop at the "objDrawingSurf ace...".

    So, how do I fix the problem. I'm about ready to give up.

    Thank you. David
  • Göran Andersson

    #2
    Re: Argument out of range exception

    You are simply trying to draw outside the bitmap.

    The range for the Y parameter is from 0 to height-1, just like the error
    message tells you.

    pcnerd wrote:[color=blue]
    > I'm trying to create a program that plots randomly colored pixels on a bitmap
    > & then displays the bitmap. When I run the program, I see the pixels being
    > plotted down the left side of the form. When it gets to the bottom, the
    > "Argument out of range exception" occurs. So, obviously the program starts.
    > The Help tells me that the argument isn't within the range of values, but it
    > doesn't explain how to fix the problem. So the Help doesn't help! It doesn't
    > tell me what the range of values are. What are the range of values? The
    > problem is with the "Y" parameter. The debugger tells me that the value
    > cannot be negative & must be less than Height.
    >
    > Here follows the source code:
    >
    > Private Sub PointillismTool StripMenuItem_C lick(ByVal sender As
    > System.Object, ByVal e As System.EventArg s) Handles
    > PointillismTool StripMenuItem.C lick
    >
    > Dim objGraphics As Graphics
    > Dim objRandom As System.Random
    > Dim RedColor As Integer
    > Dim GreenColor As Integer
    > Dim BlueColor As Integer
    > Dim Transparency As Integer
    >
    > ' Create a Graphics object using the memory bitmap.
    > objGraphics = Graphics.FromIm age(objDrawingS urface)
    >
    > ' Initialize the Random object.
    > objRandom = New Random(Now.Mill isecond)
    >
    > Transparency = objRandom.Next( 0, 256)
    >
    > Dim Xcount As Integer
    > Dim Ycount As Integer
    >
    > For Xcount = 1 To 1280
    > For Ycount = 1 To 964
    > RedColor = objRandom.Next( 0, 256)
    > GreenColor = objRandom.Next( 0, 256)
    > BlueColor = objRandom.Next( 0, 256)
    > objDrawingSurfa ce.SetPixel(Xco unt, Ycount, Color.FromArgb( RedColor,
    > GreenColor, BlueColor))
    > ' Draw myBitmap to the screen.
    > Me.CreateGraphi cs.DrawImage(ob jDrawingSurface , 0, 0)
    > Next Ycount
    > Next Xcount
    >
    > End Sub
    >
    > The problem occurs within the "For Ycount" loop at the "objDrawingSurf ace...".
    >
    > So, how do I fix the problem. I'm about ready to give up.
    >
    > Thank you. David[/color]

    Comment

    • pcnerd

      #3
      Re: Argument out of range exception


      I came up with an alternative way. I originally used nested For-Next loops.
      The inner loop executes in its entirety before the outside loop. So, the Y
      coordinate goes from 0 to 964 & then exits the For-Y loop. The next time the
      Y value is already at its maximum so the error pops up. So, I used just one
      For-Next loop & set the X & Y coordinates in the SetPixel to random values.
      It works! I have a pretty fast PC, but the pixels take forever to plot. It
      takes like 10 to 15 seconds just to draw a few thousand pixels. The VB6
      program can draw tens of thousands in a couple of seconds. I tried basically
      the same program in VB6 & the pixels were drawn very quickly. I guess that
      the reason that VB.NET is so slow with graphics is because every thing is an
      object. Is there any way to speed up the process?

      In a like question. Now that I know how to draw pixels at random, how do I
      draw them in sequence? I want to be able to display a bitmap that I've
      created one line at a time from the top of the form to the botom. How do I do
      that?

      "Göran Andersson" wrote:
      [color=blue]
      > You are simply trying to draw outside the bitmap.
      >
      > The range for the Y parameter is from 0 to height-1, just like the error
      > message tells you.
      >
      > pcnerd wrote:[color=green]
      > > I'm trying to create a program that plots randomly colored pixels on a bitmap
      > > & then displays the bitmap. When I run the program, I see the pixels being
      > > plotted down the left side of the form. When it gets to the bottom, the
      > > "Argument out of range exception" occurs. So, obviously the program starts.
      > > The Help tells me that the argument isn't within the range of values, but it
      > > doesn't explain how to fix the problem. So the Help doesn't help! It doesn't
      > > tell me what the range of values are. What are the range of values? The
      > > problem is with the "Y" parameter. The debugger tells me that the value
      > > cannot be negative & must be less than Height.
      > >
      > > Here follows the source code:
      > >
      > > Private Sub PointillismTool StripMenuItem_C lick(ByVal sender As
      > > System.Object, ByVal e As System.EventArg s) Handles
      > > PointillismTool StripMenuItem.C lick
      > >
      > > Dim objGraphics As Graphics
      > > Dim objRandom As System.Random
      > > Dim RedColor As Integer
      > > Dim GreenColor As Integer
      > > Dim BlueColor As Integer
      > > Dim Transparency As Integer
      > >
      > > ' Create a Graphics object using the memory bitmap.
      > > objGraphics = Graphics.FromIm age(objDrawingS urface)
      > >
      > > ' Initialize the Random object.
      > > objRandom = New Random(Now.Mill isecond)
      > >
      > > Transparency = objRandom.Next( 0, 256)
      > >
      > > Dim Xcount As Integer
      > > Dim Ycount As Integer
      > >
      > > For Xcount = 1 To 1280
      > > For Ycount = 1 To 964
      > > RedColor = objRandom.Next( 0, 256)
      > > GreenColor = objRandom.Next( 0, 256)
      > > BlueColor = objRandom.Next( 0, 256)
      > > objDrawingSurfa ce.SetPixel(Xco unt, Ycount, Color.FromArgb( RedColor,
      > > GreenColor, BlueColor))
      > > ' Draw myBitmap to the screen.
      > > Me.CreateGraphi cs.DrawImage(ob jDrawingSurface , 0, 0)
      > > Next Ycount
      > > Next Xcount
      > >
      > > End Sub
      > >
      > > The problem occurs within the "For Ycount" loop at the "objDrawingSurf ace...".
      > >
      > > So, how do I fix the problem. I'm about ready to give up.
      > >
      > > Thank you. David[/color]
      >[/color]

      Comment

      • Göran Andersson

        #4
        Re: Argument out of range exception

        pcnerd wrote:[color=blue]
        > I came up with an alternative way. I originally used nested For-Next loops.
        > The inner loop executes in its entirety before the outside loop. So, the Y
        > coordinate goes from 0 to 964 & then exits the For-Y loop. The next time the
        > Y value is already at its maximum so the error pops up.[/color]

        No, that's not correct.

        If you specify a loop to go from a specific value to another, it will
        always use the same values, even if the loop is inside another loop. The
        loop counter is always initialized at the start of the loop.
        [color=blue]
        > So, I used just one
        > For-Next loop & set the X & Y coordinates in the SetPixel to random values.
        > It works! I have a pretty fast PC, but the pixels take forever to plot. It
        > takes like 10 to 15 seconds just to draw a few thousand pixels.[/color]

        That's because you are doing quite a lot more than just plotting the
        pixels. You are drawing the entire bitmap to the screen for every pixel
        that you plot.

        <question type="rethorica l">

        What do you think is taking longer in the loop, plotting the pixel or
        drawing the bitmap?

        </question>
        [color=blue]
        > The VB6
        > program can draw tens of thousands in a couple of seconds. I tried basically
        > the same program in VB6 & the pixels were drawn very quickly. I guess that
        > the reason that VB.NET is so slow with graphics is because every thing is an
        > object. Is there any way to speed up the process?
        >
        > In a like question. Now that I know how to draw pixels at random, how do I
        > draw them in sequence?[/color]

        Use a loop. Like you did from the start.
        [color=blue]
        > I want to be able to display a bitmap that I've
        > created one line at a time from the top of the form to the botom. How do I do
        > that?
        >
        > "Göran Andersson" wrote:
        >[color=green]
        >> You are simply trying to draw outside the bitmap.
        >>
        >> The range for the Y parameter is from 0 to height-1, just like the error
        >> message tells you.
        >>
        >> pcnerd wrote:[color=darkred]
        >>> I'm trying to create a program that plots randomly colored pixels on a bitmap
        >>> & then displays the bitmap. When I run the program, I see the pixels being
        >>> plotted down the left side of the form. When it gets to the bottom, the
        >>> "Argument out of range exception" occurs. So, obviously the program starts.
        >>> The Help tells me that the argument isn't within the range of values, but it
        >>> doesn't explain how to fix the problem. So the Help doesn't help! It doesn't
        >>> tell me what the range of values are. What are the range of values? The
        >>> problem is with the "Y" parameter. The debugger tells me that the value
        >>> cannot be negative & must be less than Height.
        >>>
        >>> Here follows the source code:
        >>>
        >>> Private Sub PointillismTool StripMenuItem_C lick(ByVal sender As
        >>> System.Object, ByVal e As System.EventArg s) Handles
        >>> PointillismTool StripMenuItem.C lick
        >>>
        >>> Dim objGraphics As Graphics
        >>> Dim objRandom As System.Random
        >>> Dim RedColor As Integer
        >>> Dim GreenColor As Integer
        >>> Dim BlueColor As Integer
        >>> Dim Transparency As Integer
        >>>
        >>> ' Create a Graphics object using the memory bitmap.
        >>> objGraphics = Graphics.FromIm age(objDrawingS urface)
        >>>
        >>> ' Initialize the Random object.
        >>> objRandom = New Random(Now.Mill isecond)
        >>>
        >>> Transparency = objRandom.Next( 0, 256)
        >>>
        >>> Dim Xcount As Integer
        >>> Dim Ycount As Integer
        >>>
        >>> For Xcount = 1 To 1280
        >>> For Ycount = 1 To 964
        >>> RedColor = objRandom.Next( 0, 256)
        >>> GreenColor = objRandom.Next( 0, 256)
        >>> BlueColor = objRandom.Next( 0, 256)
        >>> objDrawingSurfa ce.SetPixel(Xco unt, Ycount, Color.FromArgb( RedColor,
        >>> GreenColor, BlueColor))
        >>> ' Draw myBitmap to the screen.
        >>> Me.CreateGraphi cs.DrawImage(ob jDrawingSurface , 0, 0)
        >>> Next Ycount
        >>> Next Xcount
        >>>
        >>> End Sub
        >>>
        >>> The problem occurs within the "For Ycount" loop at the "objDrawingSurf ace...".
        >>>
        >>> So, how do I fix the problem. I'm about ready to give up.
        >>>
        >>> Thank you. David[/color][/color][/color]

        Comment

        • pcnerd

          #5
          Re: Argument out of range exception


          I tried the "o to height -1" & the program started plotting the pixels, but
          partway down the form, the program locked up! I GIVE UP! VB.NET is too
          complicated for me! I'm going back to VB6. VB6 isn't perfect. It has it's
          flaws, but it's easier for me to understand. I uninstalled all of the
          software & I deleted all of the VB.NET demo programs. I doubt if I will
          reinstall the software. I'm sorry. If some future version of Windows doesn't
          support "classic" VB, then I'll have to come up with an alternative. For all
          I know, Microsoft may come up with a non .NET programming language.

          So, I'll be sending e-mails to the "classic" VB newsgroups.

          There's always Java. I've seen some pretty neat games & applets.

          I'm sorry.

          "Göran Andersson" wrote:
          [color=blue]
          > pcnerd wrote:[color=green]
          > > I came up with an alternative way. I originally used nested For-Next loops.
          > > The inner loop executes in its entirety before the outside loop. So, the Y
          > > coordinate goes from 0 to 964 & then exits the For-Y loop. The next time the
          > > Y value is already at its maximum so the error pops up.[/color]
          >
          > No, that's not correct.
          >
          > If you specify a loop to go from a specific value to another, it will
          > always use the same values, even if the loop is inside another loop. The
          > loop counter is always initialized at the start of the loop.
          >[color=green]
          > > So, I used just one
          > > For-Next loop & set the X & Y coordinates in the SetPixel to random values.
          > > It works! I have a pretty fast PC, but the pixels take forever to plot. It
          > > takes like 10 to 15 seconds just to draw a few thousand pixels.[/color]
          >
          > That's because you are doing quite a lot more than just plotting the
          > pixels. You are drawing the entire bitmap to the screen for every pixel
          > that you plot.
          >
          > <question type="rethorica l">
          >
          > What do you think is taking longer in the loop, plotting the pixel or
          > drawing the bitmap?
          >
          > </question>
          >[color=green]
          > > The VB6
          > > program can draw tens of thousands in a couple of seconds. I tried basically
          > > the same program in VB6 & the pixels were drawn very quickly. I guess that
          > > the reason that VB.NET is so slow with graphics is because every thing is an
          > > object. Is there any way to speed up the process?
          > >
          > > In a like question. Now that I know how to draw pixels at random, how do I
          > > draw them in sequence?[/color]
          >
          > Use a loop. Like you did from the start.
          >[color=green]
          > > I want to be able to display a bitmap that I've
          > > created one line at a time from the top of the form to the botom. How do I do
          > > that?
          > >
          > > "Göran Andersson" wrote:
          > >[color=darkred]
          > >> You are simply trying to draw outside the bitmap.
          > >>
          > >> The range for the Y parameter is from 0 to height-1, just like the error
          > >> message tells you.
          > >>
          > >> pcnerd wrote:
          > >>> I'm trying to create a program that plots randomly colored pixels on a bitmap
          > >>> & then displays the bitmap. When I run the program, I see the pixels being
          > >>> plotted down the left side of the form. When it gets to the bottom, the
          > >>> "Argument out of range exception" occurs. So, obviously the program starts.
          > >>> The Help tells me that the argument isn't within the range of values, but it
          > >>> doesn't explain how to fix the problem. So the Help doesn't help! It doesn't
          > >>> tell me what the range of values are. What are the range of values? The
          > >>> problem is with the "Y" parameter. The debugger tells me that the value
          > >>> cannot be negative & must be less than Height.
          > >>>
          > >>> Here follows the source code:
          > >>>
          > >>> Private Sub PointillismTool StripMenuItem_C lick(ByVal sender As
          > >>> System.Object, ByVal e As System.EventArg s) Handles
          > >>> PointillismTool StripMenuItem.C lick
          > >>>
          > >>> Dim objGraphics As Graphics
          > >>> Dim objRandom As System.Random
          > >>> Dim RedColor As Integer
          > >>> Dim GreenColor As Integer
          > >>> Dim BlueColor As Integer
          > >>> Dim Transparency As Integer
          > >>>
          > >>> ' Create a Graphics object using the memory bitmap.
          > >>> objGraphics = Graphics.FromIm age(objDrawingS urface)
          > >>>
          > >>> ' Initialize the Random object.
          > >>> objRandom = New Random(Now.Mill isecond)
          > >>>
          > >>> Transparency = objRandom.Next( 0, 256)
          > >>>
          > >>> Dim Xcount As Integer
          > >>> Dim Ycount As Integer
          > >>>
          > >>> For Xcount = 1 To 1280
          > >>> For Ycount = 1 To 964
          > >>> RedColor = objRandom.Next( 0, 256)
          > >>> GreenColor = objRandom.Next( 0, 256)
          > >>> BlueColor = objRandom.Next( 0, 256)
          > >>> objDrawingSurfa ce.SetPixel(Xco unt, Ycount, Color.FromArgb( RedColor,
          > >>> GreenColor, BlueColor))
          > >>> ' Draw myBitmap to the screen.
          > >>> Me.CreateGraphi cs.DrawImage(ob jDrawingSurface , 0, 0)
          > >>> Next Ycount
          > >>> Next Xcount
          > >>>
          > >>> End Sub
          > >>>
          > >>> The problem occurs within the "For Ycount" loop at the "objDrawingSurf ace...".
          > >>>
          > >>> So, how do I fix the problem. I'm about ready to give up.
          > >>>
          > >>> Thank you. David[/color][/color]
          >[/color]

          Comment

          • Göran Andersson

            #6
            Re: Argument out of range exception

            pcnerd wrote:[color=blue]
            > I tried the "o to height -1" & the program started plotting the pixels, but
            > partway down the form, the program locked up![/color]

            Most likely because you are creating more than a million Graphics
            objects that you don't dispose of properly.

            From MSDN on the CreateGraphics method:

            "The returned Graphics must be disposed through a call to its Dispose
            method when it is no longer needed."
            [color=blue]
            > I GIVE UP! VB.NET is too
            > complicated for me! I'm going back to VB6. VB6 isn't perfect. It has it's
            > flaws, but it's easier for me to understand.[/color]

            That is only because you are used to VB6, and not to VB.NET.

            Although the .NET framework contains far more than the VB6 runtime
            library, it's easier to understand and use as everything is arranged in
            a logical and consistent way.
            [color=blue]
            > I uninstalled all of the
            > software & I deleted all of the VB.NET demo programs. I doubt if I will
            > reinstall the software. I'm sorry.[/color]

            I think that you give up too easily. Did you never encounter any
            problems learning VB6?
            [color=blue]
            > If some future version of Windows doesn't
            > support "classic" VB, then I'll have to come up with an alternative. For all
            > I know, Microsoft may come up with a non .NET programming language.[/color]

            I believe the future of non-.NET programs will be similar to how DOS
            programs are handled nowadays. You will be able to run them, but they
            will run in an emulated environment, not as a normal program.
            [color=blue]
            > So, I'll be sending e-mails to the "classic" VB newsgroups.
            >
            > There's always Java. I've seen some pretty neat games & applets.[/color]

            If you try Java, you will see that it's rather similar to .NET.
            [color=blue]
            > I'm sorry.
            >
            > "Göran Andersson" wrote:
            >[color=green]
            >> pcnerd wrote:[color=darkred]
            >>> I came up with an alternative way. I originally used nested For-Next loops.
            >>> The inner loop executes in its entirety before the outside loop. So, the Y
            >>> coordinate goes from 0 to 964 & then exits the For-Y loop. The next time the
            >>> Y value is already at its maximum so the error pops up.[/color]
            >> No, that's not correct.
            >>
            >> If you specify a loop to go from a specific value to another, it will
            >> always use the same values, even if the loop is inside another loop. The
            >> loop counter is always initialized at the start of the loop.
            >>[color=darkred]
            >>> So, I used just one
            >>> For-Next loop & set the X & Y coordinates in the SetPixel to random values.
            >>> It works! I have a pretty fast PC, but the pixels take forever to plot. It
            >>> takes like 10 to 15 seconds just to draw a few thousand pixels.[/color]
            >> That's because you are doing quite a lot more than just plotting the
            >> pixels. You are drawing the entire bitmap to the screen for every pixel
            >> that you plot.
            >>
            >> <question type="rethorica l">
            >>
            >> What do you think is taking longer in the loop, plotting the pixel or
            >> drawing the bitmap?
            >>
            >> </question>
            >>[color=darkred]
            >>> The VB6
            >>> program can draw tens of thousands in a couple of seconds. I tried basically
            >>> the same program in VB6 & the pixels were drawn very quickly. I guess that
            >>> the reason that VB.NET is so slow with graphics is because every thing is an
            >>> object. Is there any way to speed up the process?
            >>>
            >>> In a like question. Now that I know how to draw pixels at random, how do I
            >>> draw them in sequence?[/color]
            >> Use a loop. Like you did from the start.
            >>[color=darkred]
            >>> I want to be able to display a bitmap that I've
            >>> created one line at a time from the top of the form to the botom. How do I do
            >>> that?
            >>>
            >>> "Göran Andersson" wrote:
            >>>
            >>>> You are simply trying to draw outside the bitmap.
            >>>>
            >>>> The range for the Y parameter is from 0 to height-1, just like the error
            >>>> message tells you.
            >>>>
            >>>> pcnerd wrote:
            >>>>> I'm trying to create a program that plots randomly colored pixels on a bitmap
            >>>>> & then displays the bitmap. When I run the program, I see the pixels being
            >>>>> plotted down the left side of the form. When it gets to the bottom, the
            >>>>> "Argument out of range exception" occurs. So, obviously the program starts.
            >>>>> The Help tells me that the argument isn't within the range of values, but it
            >>>>> doesn't explain how to fix the problem. So the Help doesn't help! It doesn't
            >>>>> tell me what the range of values are. What are the range of values? The
            >>>>> problem is with the "Y" parameter. The debugger tells me that the value
            >>>>> cannot be negative & must be less than Height.
            >>>>>
            >>>>> Here follows the source code:
            >>>>>
            >>>>> Private Sub PointillismTool StripMenuItem_C lick(ByVal sender As
            >>>>> System.Object, ByVal e As System.EventArg s) Handles
            >>>>> PointillismTool StripMenuItem.C lick
            >>>>>
            >>>>> Dim objGraphics As Graphics
            >>>>> Dim objRandom As System.Random
            >>>>> Dim RedColor As Integer
            >>>>> Dim GreenColor As Integer
            >>>>> Dim BlueColor As Integer
            >>>>> Dim Transparency As Integer
            >>>>>
            >>>>> ' Create a Graphics object using the memory bitmap.
            >>>>> objGraphics = Graphics.FromIm age(objDrawingS urface)
            >>>>>
            >>>>> ' Initialize the Random object.
            >>>>> objRandom = New Random(Now.Mill isecond)
            >>>>>
            >>>>> Transparency = objRandom.Next( 0, 256)
            >>>>>
            >>>>> Dim Xcount As Integer
            >>>>> Dim Ycount As Integer
            >>>>>
            >>>>> For Xcount = 1 To 1280
            >>>>> For Ycount = 1 To 964
            >>>>> RedColor = objRandom.Next( 0, 256)
            >>>>> GreenColor = objRandom.Next( 0, 256)
            >>>>> BlueColor = objRandom.Next( 0, 256)
            >>>>> objDrawingSurfa ce.SetPixel(Xco unt, Ycount, Color.FromArgb( RedColor,
            >>>>> GreenColor, BlueColor))
            >>>>> ' Draw myBitmap to the screen.
            >>>>> Me.CreateGraphi cs.DrawImage(ob jDrawingSurface , 0, 0)
            >>>>> Next Ycount
            >>>>> Next Xcount
            >>>>>
            >>>>> End Sub
            >>>>>
            >>>>> The problem occurs within the "For Ycount" loop at the "objDrawingSurf ace...".
            >>>>>
            >>>>> So, how do I fix the problem. I'm about ready to give up.
            >>>>>
            >>>>> Thank you. David[/color][/color][/color]

            Comment

            Working...