Graphics.FromImage Error Parameter is not valid

R

RW

Hi,

A dialog form (DialogForm) contains a picture box (myPictureBox).
On load of the dialog form, I draw a 2D pie diagram in that picture box. The
data for the diagram comes from the the column [Percentage] in a SQL Server
database tabel.

I get the "Parameter is not valid" error on the dlg.ShowDialog() command:



Dim dlg As New DialogForm
dlg.ShowDialog()


Private Sub DialogForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Draw2DPie(myPictureBox)
End Sub


Private Sub Draw2DPie(ByRef aPictureBox As PictureBox)
Dim bmp As New Bitmap(aPictureBox.Width, aPictureBox.Height,
Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim penGraph As New Pen(Color.Black, 2)
Dim brushGraph As New SolidBrush(Color.Red)
Dim topGraph As Integer = 0
Dim leftGraph As Integer = 0
Dim widthGraph As Integer = aPictureBox.Width
Dim heightGraph As Integer = aPictureBox.Height

g.Clear(BackColor)
Dim startAngle As Single = -90.0
Dim sweepAngle As Single = 0.0
For nIndex As Integer = 0 To ds.Tables(0).Rows.Count - 1
sweepAngle = 360 * CType(ds.Tables(0).Rows(nIndex).Item("Percentage"),
Single)
g.DrawPie(penGraph, leftGraph, topGraph, widthGraph, heightGraph,
startAngle, sweepAngle)
g.FillPie(brushGraph, leftGraph, topGraph, widthGraph, heightGraph,
startAngle, sweepAngle)
startAngle += sweepAngle
Next
penGraph.Dispose()
brushGraph.Dispose()
g.Dispose()

aPictureBox.Image = bmp
bmp.Dispose()

End Sub


Anybody an idea what causes that error?
Thanks
 
A

Armin Zingler

RW said:
aPictureBox.Image = bmp
bmp.Dispose()

Don't know if that's the problem, but you shouldn't dispose a bitmap that is assigned
to a picturebox.
 
C

Chris Dunaway

Hi,

A dialog form (DialogForm) contains a picture box (myPictureBox).
On load of the dialog form, I draw a 2D pie diagram in that picture box. The
data for the diagram comes from the the column [Percentage] in a SQL Server
database tabel.

I get the "Parameter is not valid" error on the dlg.ShowDialog() command:

Dim dlg As New DialogForm
dlg.ShowDialog()

Private Sub DialogForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Draw2DPie(myPictureBox)
End Sub

Private Sub Draw2DPie(ByRef aPictureBox As PictureBox)
Dim bmp As New Bitmap(aPictureBox.Width, aPictureBox.Height,
Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim penGraph As New Pen(Color.Black, 2)
Dim brushGraph As New SolidBrush(Color.Red)
Dim topGraph As Integer = 0
Dim leftGraph As Integer = 0
Dim widthGraph As Integer = aPictureBox.Width
Dim heightGraph As Integer = aPictureBox.Height

g.Clear(BackColor)
Dim startAngle As Single = -90.0
Dim sweepAngle As Single = 0.0
For nIndex As Integer = 0 To ds.Tables(0).Rows.Count - 1
sweepAngle = 360 * CType(ds.Tables(0).Rows(nIndex).Item("Percentage"),
Single)
g.DrawPie(penGraph, leftGraph, topGraph, widthGraph, heightGraph,
startAngle, sweepAngle)
g.FillPie(brushGraph, leftGraph, topGraph, widthGraph, heightGraph,
startAngle, sweepAngle)
startAngle += sweepAngle
Next
penGraph.Dispose()
brushGraph.Dispose()
g.Dispose()

aPictureBox.Image = bmp
bmp.Dispose()

End Sub

Anybody an idea what causes that error?
Thanks

Can you show the stack trace for the exception?

Also, what happens if you call your Draw2DPie method from the Form's
Shown event instead?

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top