Simple drawing app "out of memory" exception

gnott

New member
Joined
Feb 13, 2006
Messages
2
Programming Experience
1-3
I am creating a very simple drawing application as an assignment for a VB.NET class. The app draws a freehand line with the mouse inside a PictureBox and allows change of color and linewidth. It works fine except when I change the line width using a combo box, then it throws an out of memory exception. Bizarre thing is. . . it only seems to throw the exception when I change the line from smaller (1 px) to larger (5 px). It doesn't throw the exception if I go from large to small!!!!

I have caught the exception on this line:

e.Graphics.DrawPath(PenDown, PenPath)

PenDown is my pen class object that is created by:

Dim PenDown As New Pen(PenColor, PenWidth)

PenPath is created with PenPath.AddLine(e.X, e.Y, e.X, e.Y)

The PenWidth property of PenDown is being assigned from a combo box with the statement:

PenWidth = CSng(PenSize.SelectedIndex + 1)

Pensize is the name of the combobox. +1 because the SelectedIndex starts at 0. The CSng() explicitly converts the return from the combobox to SINGLE data type which is what the Pen class requires. The conversion is not necessary and it is done implicitly but it's one of the many steps I have taken to solve my problem.

I have taken many steps to try to solve this issue including consulting a professional VB.Net coder I found on CraigsList. Anyone?????

Many thanks.

Graham
 
Assuming your combobox is populated with numbers to choose the size of your line. I wonder why you are using the selected index property of the combobox. The Selected item property will return what item you have currently selected. The selected index will return the index of the item you have selected. Then there would be no need for you to use +1.
 
The combobox is populated with text, such as "1 Pixel, 2 Pixels. .. .", not just numbers.

Using the SelectedItem property of the combobox I would need to use a SELECT CASE to set the PenWidth. I chose the direct assignment of SelectedIndex + 1 to PenWidth just to make the code a little cleaner without the SELECT CASE. I have tried both however and still get the out of memory exception.

Thanks!

Graham
 
Back
Top