saving icon to a file

  • Thread starter Thread starter Jo Franklin
  • Start date Start date
J

Jo Franklin

I am trying to save an Icon to a file. My code to extract and display the
icon works correctly, but I am unable to save it to a file. Can someone
please help with the code?
Thank you in advance.

ExtractIconEx(sFile, 1, IconPtrLarge, IconPtrSmall, 1)
myIcon = Icon.FromHandle(IconPtrLarge)
ImageListLarge.Images.Add(myIcon)
Dim iconFile As New IO.FileStream("C:\Icon1.ico", FileMode.Create)
myIcon.Save(iconFile) <-- this line is giving me the error
iconFile.Close()
myIcon.Dispose()

This is the error I'm getting
An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in system.drawing.dll
Additional information: Unspecified error
 
Jo Franklin said:
I am trying to save an Icon to a file. My code to extract and display
the icon works correctly, but I am unable to save it to a file. Can
someone please help with the code?
Thank you in advance.

ExtractIconEx(sFile, 1, IconPtrLarge, IconPtrSmall, 1)
myIcon = Icon.FromHandle(IconPtrLarge)
ImageListLarge.Images.Add(myIcon)
Dim iconFile As New IO.FileStream("C:\Icon1.ico", FileMode.Create)
myIcon.Save(iconFile) <-- this line is giving me the error
iconFile.Close()
myIcon.Dispose()

This is the error I'm getting
An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
system.drawing.dll Additional information: Unspecified error

If you catch the exception, does the exception object return more
information?

I tried your code, and after using the icon index 0 instead of 1, the icon
could be saved without a problem. => can't repro your problem

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin gave the solution, heres the explanation:
The last parameter of ExtractIconEx is the amount of Icons to extract.
The third and fourth parameters are arrays for iconhandles(large and small)
The first item in the array has an index of 0.

\\\
Dim IconPtrLarge() As IntPtr = {New IntPtr}
Dim IconPtrSmall() As IntPtr = {New IntPtr}
....
ExtractIconEx(sFile, 1, IconPtrLarge, IconPtrSmall, 1)
myIcon = Icon.FromHandle(IconPtrLarge(0))
....
///
 
Back
Top