Home All Groups Group Topic Archive Search About

Argument out of range exception

Author
24 Jun 2006 4:59 PM
pcnerd
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 PointillismToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PointillismToolStripMenuItem.Click

    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.FromImage(objDrawingSurface)

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

    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)
        objDrawingSurface.SetPixel(Xcount, Ycount, Color.FromArgb(RedColor,
GreenColor, BlueColor))
        ' Draw myBitmap to the screen.
        Me.CreateGraphics.DrawImage(objDrawingSurface, 0, 0)
      Next Ycount
    Next Xcount

End Sub

The problem occurs within the "For Ycount" loop at the "objDrawingSurface...".

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

Thank you. David

Author
24 Jun 2006 6:35 PM
Göran Andersson
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:
Show quoteHide quote
> 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 PointillismToolStripMenuItem_Click(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> PointillismToolStripMenuItem.Click
>
>     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.FromImage(objDrawingSurface)
>
>     ' Initialize the Random object.
>     objRandom = New Random(Now.Millisecond)
>
>     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)
>         objDrawingSurface.SetPixel(Xcount, Ycount, Color.FromArgb(RedColor,
> GreenColor, BlueColor))
>         ' Draw myBitmap to the screen.
>         Me.CreateGraphics.DrawImage(objDrawingSurface, 0, 0)
>       Next Ycount
>     Next Xcount
>
> End Sub
>
> The problem occurs within the "For Ycount" loop at the "objDrawingSurface...".
>
> So, how do I fix the problem. I'm about ready to give up.
>
> Thank you. David
Author
25 Jun 2006 3:23 AM
pcnerd
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?

Show quoteHide quote
"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 PointillismToolStripMenuItem_Click(ByVal sender As
> > System.Object, ByVal e As System.EventArgs) Handles
> > PointillismToolStripMenuItem.Click
> >
> >     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.FromImage(objDrawingSurface)
> >
> >     ' Initialize the Random object.
> >     objRandom = New Random(Now.Millisecond)
> >
> >     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)
> >         objDrawingSurface.SetPixel(Xcount, Ycount, Color.FromArgb(RedColor,
> > GreenColor, BlueColor))
> >         ' Draw myBitmap to the screen.
> >         Me.CreateGraphics.DrawImage(objDrawingSurface, 0, 0)
> >       Next Ycount
> >     Next Xcount
> >
> > End Sub
> >
> > The problem occurs within the "For Ycount" loop at the "objDrawingSurface...".
> >
> > So, how do I fix the problem. I'm about ready to give up.
> >
> > Thank you. David
>
Author
25 Jun 2006 1:47 PM
Göran Andersson
pcnerd wrote:
> 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.

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.

> 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.

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="rethorical">

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

</question>

> 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?

Use a loop. Like you did from the start.

Show quoteHide quote
> 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 PointillismToolStripMenuItem_Click(ByVal sender As
>>> System.Object, ByVal e As System.EventArgs) Handles
>>> PointillismToolStripMenuItem.Click
>>>
>>>     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.FromImage(objDrawingSurface)
>>>
>>>     ' Initialize the Random object.
>>>     objRandom = New Random(Now.Millisecond)
>>>
>>>     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)
>>>         objDrawingSurface.SetPixel(Xcount, Ycount, Color.FromArgb(RedColor,
>>> GreenColor, BlueColor))
>>>         ' Draw myBitmap to the screen.
>>>         Me.CreateGraphics.DrawImage(objDrawingSurface, 0, 0)
>>>       Next Ycount
>>>     Next Xcount
>>>
>>> End Sub
>>>
>>> The problem occurs within the "For Ycount" loop at the "objDrawingSurface...".
>>>
>>> So, how do I fix the problem. I'm about ready to give up.
>>>
>>> Thank you. David
Author
25 Jun 2006 5:56 PM
pcnerd
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.

Show quoteHide quote
"Göran Andersson" wrote:

> pcnerd wrote:
> > 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.
>
> 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.
>
> > 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.
>
> 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="rethorical">
>
> What do you think is taking longer in the loop, plotting the pixel or
> drawing the bitmap?
>
> </question>
>
> > 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?
>
> Use a loop. Like you did from the start.
>
> > 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 PointillismToolStripMenuItem_Click(ByVal sender As
> >>> System.Object, ByVal e As System.EventArgs) Handles
> >>> PointillismToolStripMenuItem.Click
> >>>
> >>>     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.FromImage(objDrawingSurface)
> >>>
> >>>     ' Initialize the Random object.
> >>>     objRandom = New Random(Now.Millisecond)
> >>>
> >>>     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)
> >>>         objDrawingSurface.SetPixel(Xcount, Ycount, Color.FromArgb(RedColor,
> >>> GreenColor, BlueColor))
> >>>         ' Draw myBitmap to the screen.
> >>>         Me.CreateGraphics.DrawImage(objDrawingSurface, 0, 0)
> >>>       Next Ycount
> >>>     Next Xcount
> >>>
> >>> End Sub
> >>>
> >>> The problem occurs within the "For Ycount" loop at the "objDrawingSurface...".
> >>>
> >>> So, how do I fix the problem. I'm about ready to give up.
> >>>
> >>> Thank you. David
>
Author
25 Jun 2006 8:53 PM
Göran Andersson
pcnerd wrote:
> I tried the "o to height -1" & the program started  plotting the pixels, but
> partway down the form, the program locked up!

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."

> 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.

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.

> 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.

I think that you give up too easily. Did you never encounter any
problems learning VB6?

> 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.

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.

> So, I'll be sending e-mails to the "classic" VB newsgroups.
>
> There's always Java. I've seen some pretty neat games & applets.

If you try Java, you will see that it's rather similar to .NET.

Show quoteHide quote
> I'm sorry.
>
> "Göran Andersson" wrote:
>
>> pcnerd wrote:
>>> 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.
>> 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.
>>
>>> 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.
>> 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="rethorical">
>>
>> What do you think is taking longer in the loop, plotting the pixel or
>> drawing the bitmap?
>>
>> </question>
>>
>>> 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?
>> Use a loop. Like you did from the start.
>>
>>> 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 PointillismToolStripMenuItem_Click(ByVal sender As
>>>>> System.Object, ByVal e As System.EventArgs) Handles
>>>>> PointillismToolStripMenuItem.Click
>>>>>
>>>>>     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.FromImage(objDrawingSurface)
>>>>>
>>>>>     ' Initialize the Random object.
>>>>>     objRandom = New Random(Now.Millisecond)
>>>>>
>>>>>     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)
>>>>>         objDrawingSurface.SetPixel(Xcount, Ycount, Color.FromArgb(RedColor,
>>>>> GreenColor, BlueColor))
>>>>>         ' Draw myBitmap to the screen.
>>>>>         Me.CreateGraphics.DrawImage(objDrawingSurface, 0, 0)
>>>>>       Next Ycount
>>>>>     Next Xcount
>>>>>
>>>>> End Sub
>>>>>
>>>>> The problem occurs within the "For Ycount" loop at the "objDrawingSurface...".
>>>>>
>>>>> So, how do I fix the problem. I'm about ready to give up.
>>>>>
>>>>> Thank you. David