GDI+ Crashing!

S

Smokey Grindel

I have an app that runs in the system tray. It takes an icon from the
programs resources then draws two rectangles on it and numbers... the wierd
thing is its crashing... here is the exception I am getting


System.ArgumentException: Parameter is not valid.
at System.Drawing.Biutmap..ctor(Int32 width, Int32 height, PixelFormat
format)
at System.Drawing.Icon.ToBitmap()
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at
System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception
t)
....

here is the code where it is crashing... anyone catch anything wrong? I know
its a mess its just "proof of concept" type code writen quickly

this crash doesnt always happen... it will run all day fine but at night it
just seems to crash...

Thanks!



Using fntLarge As New Font("Arial", 12, FontStyle.Regular,
GraphicsUnit.Pixel), fntSmall As New Font("Arial", 8, FontStyle.Regular,
GraphicsUnit.Pixel)

' draw icon numbers

Using bmp As Bitmap = New Drawing.Icon(My.Resources.star_yellow, New
Size(16, 16)).ToBitmap

Using g As Graphics = Graphics.FromImage(bmp)

' do ind count first

g.SmoothingMode = Drawing2D.SmoothingMode.None

g.TextRenderingHint =
Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit

If IndCount > 0 Then

If IndCount > 9 Then

Dim rect As New RectangleF(New PointF(9, 5), New Size(7, 11))

Using sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

sf.FormatFlags = StringFormatFlags.DirectionVertical

g.FillRectangle(Brushes.Blue, rect)

g.DrawString(IndCount.ToString, fntSmall, Brushes.White, New RectangleF(6,
2, 9, 16), sf)

End Using

Else

g.FillRectangle(Brushes.Blue, New RectangleF(New PointF(9, 5), New Size(7,
11)))

g.DrawString(IndCount.ToString.Trim, fntLarge, Brushes.White, 7, 3)

End If

End If

' do mc count second

If MCCount > 0 Then

If MCCount > 9 Then

Dim rect As New RectangleF(New PointF(0, 5), New Size(7, 15))

Using sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

sf.FormatFlags = StringFormatFlags.DirectionVertical

g.FillRectangle(Brushes.DarkGreen, rect)

g.DrawString(MCCount.ToString, fntSmall, Brushes.White, New RectangleF(-2,
2, 9, 16), sf)

End Using

Else

g.FillRectangle(Brushes.DarkGreen, New RectangleF(New PointF(0, 5), New
Size(7, 15)))

g.DrawString(MCCount.ToString, fntLarge, Brushes.White, -1, 3)

End If

End If

ntfIcon.Icon = Icon.FromHandle(bmp.GetHicon)

End Using

End Using

End Using
 
B

Branco Medeiros

Smokey said:
I have an app that runs in the system tray. It takes an icon from the
programs resources then draws two rectangles on it and numbers... the wierd
thing is its crashing... here is the exception I am getting

System.ArgumentException: Parameter is not valid.
at System.Drawing.Biutmap..ctor(Int32 width, Int32 height, PixelFormat
format)
at System.Drawing.Icon.ToBitmap()
Using bmp As Bitmap = New Drawing.Icon(My.Resources.star_yellow, New
Size(16, 16)).ToBitmap
<snip>

It seems you're not disposing the temporary Icon you create to extract
the bitmap from. If you're doing this a lot of times, it could explain
the crash after a long run (all other things staying the same).

If you're going to recreate this icon a gazillion times during the app
lifetime, I suggest you create it just once, when the app starts, and
place it somewhere your code can reach (actually, you could do the
same with fntSmall and fntLarge)...

Otherwise use something like:

Using Bmp as Bitmap
Using I as New Drawing.Icon(....)
Bmp = I.ToBitmap
End Using
'...

HTH.

Regards,

Branco.
 
S

Smokey Grindel

cant believe I didn't catch that... thanks! also moved the font definitions
up to a private member of the class so they are only created once with the
class instance instead of each time.. Hopefully this was the problem
 
S

Smokey Grindel

humm still doesnt seem to work... crashes randomly at night, here is the
updated code...

' draw icon numbers

Using icn As New Drawing.Icon(My.Resources.star_yellow, New Size(16, 16))

Using bmp As Bitmap = icn.ToBitmap

Using g As Graphics = Graphics.FromImage(bmp)

' do indi count first

g.SmoothingMode = Drawing2D.SmoothingMode.None

g.TextRenderingHint =
Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit

If IndiCount > 0 Then

If IndiCount > 9 Then

Dim rect As New RectangleF(New PointF(9, 5), New Size(7, 11))

Using sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

sf.FormatFlags = StringFormatFlags.DirectionVertical

g.FillRectangle(Brushes.Blue, rect)

g.DrawString(IndiCount.ToString, fntSmall, Brushes.White, New RectangleF(6,
2, 9, 16), sf)

End Using

Else

g.FillRectangle(Brushes.Blue, New RectangleF(New PointF(9, 5), New Size(7,
11)))

g.DrawString(IndiCount.ToString.Trim, fntLarge, Brushes.White, 7, 3)

End If

End If

' do mc count second

If MCCount > 0 Then

If MCCount > 9 Then

Dim rect As New RectangleF(New PointF(0, 5), New Size(7, 15))

Using sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

sf.FormatFlags = StringFormatFlags.DirectionVertical

g.FillRectangle(Brushes.DarkGreen, rect)

g.DrawString(MCCount.ToString, fntSmall, Brushes.White, New RectangleF(-2,
2, 9, 16), sf)

End Using

Else

g.FillRectangle(Brushes.DarkGreen, New RectangleF(New PointF(0, 5), New
Size(7, 15)))

g.DrawString(MCCount.ToString, fntLarge, Brushes.White, -1, 3)

End If

End If

ntfIcon.Icon = Icon.FromHandle(bmp.GetHicon)

End Using

End Using

End Using


crashes with the same error...
 

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