473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

GDI+ Crashing!

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(E xception t)
at
System.Windows.Forms.Application.ThreadContext.OnT hreadException(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.SingleBitPerPixelGr idFit

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
Mar 23 '07 #1
3 4057
Smokey Grindel wrote:
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()
<snip>
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.

Mar 23 '07 #2
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

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
Smokey Grindel wrote:
>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()
<snip>
>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.

Mar 23 '07 #3
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.SingleBitPerPixelGr idFit

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


"Smokey Grindel" <no****@nospam.comwrote in message
news:ei**************@TK2MSFTNGP02.phx.gbl...
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

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
>Smokey Grindel wrote:
>>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()
<snip>
>>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.


Mar 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: **ham | last post by:
I know that's an old dirty issue; GDI+ almost -the slowest part of the framework - has bothered many developers using it in animations. Even in managed C++ the performance is awful. Now, any dude...
6
by: James dean | last post by:
I have heard that the video drivers in GDI+ are a big performance issue. But is this only an issue with something like Games Programming i think...is this wrong?. What about a drawing application...
1
by: James dean | last post by:
Could someone explain how this works. I think the graphics card is used to do blitting and drawing shapes like rectangles. How does it draw using the Graphics card on the PC and why is this feature...
6
by: James dean | last post by:
I want a good site that will show clearly how much more functionality GDI+ has. I cannot seem to find anything other than sites that list "some" of the new functionality that GDI+ offers. A...
7
by: | last post by:
We create VC++ programs that does some GDI drawing functionality. I discovered GDI+ and this seems to be a big step forward, and appears to be standard available in Windows XP and Windows Server...
0
by: Brian Keating | last post by:
hi there i've a test program that creates a treeview and destroys it over and over, i keep track of the gdi object count for the process and see if they are ok. However when i switch on...
7
by: Marcin Rzeznicki | last post by:
Hello, Do you think it is legitimate practice to mix GDI+ and GDI calls (via Get/ReleaseHDC()) in paint event of a control? I've heard there is possibility of performance loss while "locking"...
0
by: livibetter | last post by:
I can or can not run any .net programs which using Windows.Forms At the time I just installed Windows XP Home, I can run .net programs. After sometime, I can't run those programs anymore. I just...
5
by: Jonathan Boivin | last post by:
Hi, I've got some problems with loading bills using a bill usercontrol I built. If I load all current bills in my test environment (156) everything is fine once, but repeating the operation...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.