Bitmap.GetHIcon leaking GDI resources?

S

Smokey Grindel

when I do this code
Dim HIcon As IntPtr = bmp.GetHicon to convert a Bitmap which is just a 16x16
image I pulled from a resource originally as an icon (16x16x32bit) then
converted into a bitmap to draw on it's surface then wanted to convert back
to an icon... but when I do the other two commands

Using bmp As Bitmap = My.Resources.bell.ToBitmap
Using g As Graphics = Graphics.FromImage(bmp)
end using
end using

GDI resources are released when they leave the using block. when I put the
HIcon IntPtr in there my GDI Object could is a never ending increment...
(this executes once a second to update the image, basically an animated
image when its used).. So this code causes a GDI leak

Using bmp As Bitmap = My.Resources.bell.ToBitmap
Using g As Graphics = Graphics.FromImage(bmp)
Dim HIcon As IntPtr = bmp.GetHicon
end using
end using

why? The only other code that is left after that leak is this

Using bmp As Bitmap = My.Resources.bell.ToBitmap
Using g As Graphics = Graphics.FromImage(bmp)
Dim HIcon As IntPtr = bmp.GetHicon
Using NewIcon As Icon = Icon.FromHandle(HIcon)

' Place onto drawing surface that requres an icon now...
end using
end using
end using
 
A

Armin Zingler

Smokey Grindel said:
when I do this code
Dim HIcon As IntPtr = bmp.GetHicon to convert a Bitmap which is just
a 16x16 image I pulled from a resource originally as an icon
(16x16x32bit) then converted into a bitmap to draw on it's surface
then wanted to convert back to an icon... but when I do the other
two commands

Using bmp As Bitmap = My.Resources.bell.ToBitmap
Using g As Graphics = Graphics.FromImage(bmp)
end using
end using

GDI resources are released when they leave the using block. when I
put the HIcon IntPtr in there my GDI Object could is a never ending
increment... (this executes once a second to update the image,
basically an animated image when its used).. So this code causes a
GDI leak

You have to call DestroyIcon (API function) to destroy the icon. Have a look
at the example in the help for GetHIcon method.


Armin
 
S

Smokey Grindel

that would be exactly what I missed. thanks!

Armin Zingler said:
You have to call DestroyIcon (API function) to destroy the icon. Have a
look at the example in the help for GetHIcon method.


Armin
 

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

Similar Threads


Top