none
How to get the icon of file extension? RRS feed

  • Question

  • I want to get the icon of file extension (for example .pdf), I use the way below, but I feel it's stupid.

    1. Create a temp file (c:\temp.pdf)

    2. Get the default icon of this file using SHGetFileInfo.

    3. Delete this file.

    Is there better way? thank you.

    Friday, February 22, 2019 3:36 AM

Answers

  • For example, with ".pdf" :

    Dim sfi As New SHFILEINFO
    Dim hImageList As IntPtr = SHGetFileInfo(".pdf", 0, sfi, CUInt(Marshal.SizeOf(sfi)), SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    Dim ico As Icon = Icon.FromHandle(sfi.hIcon)
    ' Test drawing icon on desktop
    Using gr As Graphics = Graphics.FromHwnd(IntPtr.Zero)
        gr.DrawIcon(ico, 50, 50)
    End Using

    with declarations :

    Public Const SHGFI_ICON = &H100  ' Get icon
    Public Const SHGFI_DISPLAYNAME = &H200  ' Get display name
    Public Const SHGFI_TYPENAME = &H400  ' Get type name
    Public Const SHGFI_ATTRIBUTES = &H800  ' Get attributes
    Public Const SHGFI_ICONLOCATION = &H1000  ' Get icon location
    Public Const SHGFI_EXETYPE = &H2000  ' Return exe type
    Public Const SHGFI_SYSICONINDEX = &H4000  ' Get system icon index
    Public Const SHGFI_LINKOVERLAY = &H8000  ' put a link overlay On icon
    Public Const SHGFI_SELECTED = &H10000  ' Show() icon In selected state
    Public Const SHGFI_ATTR_SPECIFIED = &H20000 ' Get only specified attributes
    
    Public Const SHGFI_LARGEICON = &H0 ' Get large icon
    Public Const SHGFI_SMALLICON = &H1 ' Get small icon
    Public Const SHGFI_OPENICON = &H2 ' Get open icon
    Public Const SHGFI_SHELLICONSIZE = &H4 ' Get shell size icon
    Public Const SHGFI_PIDL = &H8 ' pszPath Is a pidl
    Public Const SHGFI_USEFILEATTRIBUTES = &H10 'use passed dwFileAttribute
    Public Const SHGFI_ADDOVERLAYS = &H20 ' apply the appropriate overlays
    Public Const SHGFI_OVERLAYINDEX = &H40 ' Get the index Of the overlay in the upper 8 bits of the iIcon
    
    Public Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    End Structure
    
    <DllImport("Shell32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function SHGetFileInfo(pszPath As String, dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, cbFileInfo As UInteger, uFlags As UInteger) As IntPtr
    End Function

    • Marked as answer by gaxjyxq Friday, February 22, 2019 12:24 PM
    Friday, February 22, 2019 9:50 AM

All replies

  • Hi gaxjyxq,

    I'm not sure I can understand your wants.
    Please use some freeware like "Icons from File". Or you can find others by searching web with "icon extract".

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Friday, February 22, 2019 4:05 AM
  • Hi,

    code:

     Dim fileName As String = "temp." & "pdf"
            File.Create(fileName).Close()
            Dim img As Image = System.Drawing.Icon.ExtractAssociatedIcon(fileName).ToBitmap()
            File.Delete(fileName)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 22, 2019 5:38 AM
  • Yes, you are right, will system take more resource if I will get lots of icons of file extension?
    Friday, February 22, 2019 6:24 AM
  • You don't need to create a temp file with SHGetFileInfo if you use the flag SHGFI_USEFILEATTRIBUTES
    Friday, February 22, 2019 7:34 AM
  • Can you provide the sample code? thank you.
    Friday, February 22, 2019 9:23 AM
  • For example, with ".pdf" :

    Dim sfi As New SHFILEINFO
    Dim hImageList As IntPtr = SHGetFileInfo(".pdf", 0, sfi, CUInt(Marshal.SizeOf(sfi)), SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    Dim ico As Icon = Icon.FromHandle(sfi.hIcon)
    ' Test drawing icon on desktop
    Using gr As Graphics = Graphics.FromHwnd(IntPtr.Zero)
        gr.DrawIcon(ico, 50, 50)
    End Using

    with declarations :

    Public Const SHGFI_ICON = &H100  ' Get icon
    Public Const SHGFI_DISPLAYNAME = &H200  ' Get display name
    Public Const SHGFI_TYPENAME = &H400  ' Get type name
    Public Const SHGFI_ATTRIBUTES = &H800  ' Get attributes
    Public Const SHGFI_ICONLOCATION = &H1000  ' Get icon location
    Public Const SHGFI_EXETYPE = &H2000  ' Return exe type
    Public Const SHGFI_SYSICONINDEX = &H4000  ' Get system icon index
    Public Const SHGFI_LINKOVERLAY = &H8000  ' put a link overlay On icon
    Public Const SHGFI_SELECTED = &H10000  ' Show() icon In selected state
    Public Const SHGFI_ATTR_SPECIFIED = &H20000 ' Get only specified attributes
    
    Public Const SHGFI_LARGEICON = &H0 ' Get large icon
    Public Const SHGFI_SMALLICON = &H1 ' Get small icon
    Public Const SHGFI_OPENICON = &H2 ' Get open icon
    Public Const SHGFI_SHELLICONSIZE = &H4 ' Get shell size icon
    Public Const SHGFI_PIDL = &H8 ' pszPath Is a pidl
    Public Const SHGFI_USEFILEATTRIBUTES = &H10 'use passed dwFileAttribute
    Public Const SHGFI_ADDOVERLAYS = &H20 ' apply the appropriate overlays
    Public Const SHGFI_OVERLAYINDEX = &H40 ' Get the index Of the overlay in the upper 8 bits of the iIcon
    
    Public Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    End Structure
    
    <DllImport("Shell32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function SHGetFileInfo(pszPath As String, dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, cbFileInfo As UInteger, uFlags As UInteger) As IntPtr
    End Function

    • Marked as answer by gaxjyxq Friday, February 22, 2019 12:24 PM
    Friday, February 22, 2019 9:50 AM
  • Good job, thank you.
    Friday, February 22, 2019 12:24 PM