Bitmap.GetHIcon leaking GDI resources?

  • Thread starter Thread starter Smokey Grindel
  • Start date Start date
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
 
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
 
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
 
Back
Top