Extract specific icon layer from file then save it as .ico file with transparency

前端 未结 2 1063
一整个雨季
一整个雨季 2020-12-19 11:58

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

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 12:52

    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.

    • Download the source code from the project site and compile it. Be sure to compile in 64bit if your project is 64bit and vice versa for 32.
    • Find FreeImage.dll and move it to your C# or vb.net project folder.
    • This dll cannot be referenced directly by .Net projects in Visual Studio. You will have to also build FreeImageNET.dll which you include in the same folder as FreeImage.dll and reference in Visual Studio. FreeImageNET.dll's source can be fround under Wrapper/FreeImage.NET/cs. FreeImageNET.dll can be compiled under the anycpu architecture (64+32bit) or 64/32bit indivually.

提交回复
热议问题