As the title of the question says, I\'m trying to extract a specific icon layer from file then save it as ico file with transparency (as the source icon have).
There
I was trying to reproduce your question but without your original exe or icon file, it's difficult to do so. I did have this issue in the past though. There are some instances where saving the icon in Visual Basic or C# (especially on older editions such as 2008 or 2010) ended with a 256 color icon. I ended up resolving this by going for an external image management library such as FreeImage which I plug into my project to replace the MS one.
FreeImage is 100% open source and available here: http://freeimage.sourceforge.net/ (see the instructions on how to reference it into your project below).
Here is an example of how to use it in VB.net:
Public Shared Function SHDefExtractIcon(ByVal iconFile As String,
ByVal iconIndex As Integer,
ByVal flags As UInteger,
ByRef hiconLarge As IntPtr,
ByRef hiconSmall As IntPtr,
ByVal iconSize As UInteger
) As Integer
End Function
Dim hiconLarge As IntPtr
SHDefExtractIcon("C:\file.exe", 0, 0, hiconLarge, Nothing, 256)
Dim ico As Icon = Icon.FromHandle(hiconLarge)
Dim bmp As Bitmap = ico.ToBitmap()
Dim fiBmp As FreeImageAPI.FreeImageBitmap = New FreeImageAPI.FreeImageBitmap(bmp)
' At this point you can either save a simple one size icon file or you can
' take advantage of the FreeImage engine to save a multi-sized icon:
fiBmp.Save("C:\ico1.ico", FreeImageAPI.FREE_IMAGE_FORMAT.FIF_ICO)
' To add more layers to your Icon:
fiBmp.Rescale(128, 128, FreeImageAPI.FREE_IMAGE_FILTER.FILTER_LANCZOS3)
fiBmp.SaveAdd("C:\ico1.ico")
fiBmp.Rescale(64, 64, FreeImageAPI.FREE_IMAGE_FILTER.FILTER_LANCZOS3)
fiBmp.SaveAdd("C:\ico1.ico")
fiBmp.Rescale(48, 48, FreeImageAPI.FREE_IMAGE_FILTER.FILTER_LANCZOS3)
fiBmp.SaveAdd("C:\ico1.ico")
'etc etc etc
This is how you plug FreeImage into your project.