7 Replies - 7170 Views - Last Post: 06 March 2014 - 08:14 PM Rate Topic: -----

#1 Deftware  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 09-December 13

Extracting a folder icon

Posted 06 March 2014 - 08:08 AM

hello

i want to extract the icon of a folder like C:\New Folder\

i have this code for extracting icons

  Public Function GetIconForFileOrFolder(ByVal FilePath As String) As Bitmap
        Return Drawing.Icon.ExtractAssociatedIcon(FilePath).ToBitmap()
    End Function



but it don't work with folders

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Extracting a folder icon

#2 belgarion262  Icon User is offline

  • Prince of all (2) Saiyans

Reputation: 172
  • View blog
  • Posts: 955
  • Joined: 25-October 09

Re: Extracting a folder icon

Posted 06 March 2014 - 08:46 AM

It is not possible to use Icon.ExtractAssociatedIcon on anything other than files. This API is a thin wrapper on top of the Win32 call ExtractAssociatedIcon. While the documentation for the managed code is a bit ambiguous, the native documentation is much clearer that the target must be a file. It goes further to say that it must be an executable file.

Unfortunately I'm not sure if there is an equivalent function for Directories or not.
Was This Post Helpful? 0
  • +
  • -

#3 Deftware  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 09-December 13

Re: Extracting a folder icon

Posted 06 March 2014 - 08:50 AM

View Postbelgarion262, on 06 March 2014 - 08:46 AM, said:

It is not possible to use
Icon.ExtractAssociatedIcon on anything other than files. This API is a thin wrapper on top of the Win32 call ExtractAssociatedIcon. While the documentation for the managed code is a bit ambiguous, the native documentation is much clearer that the target must be a file. It goes further to say that it must be an executable file.

Unfortunately I'm not sure if there is an equivalent function for Directories or not.


But programs such as winrar has windows like folders. And do you know a good folder image i can use?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Extracting a folder icon

Posted 06 March 2014 - 08:55 AM

have you tried google?

Folder Icons would be a good search and then click the images to see a whole slew of folder icons.

You could also pull them out of the Shell32.dll using some icon extractor or write your own.

This post has been edited by CharlieMay: 06 March 2014 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13285
  • View blog
  • Posts: 52,806
  • Joined: 12-June 08

Re: Extracting a folder icon

Posted 06 March 2014 - 08:55 AM

Have you tried searching "free folder icon" ?
Was This Post Helpful? 0
  • +
  • -

#6 belgarion262  Icon User is offline

  • Prince of all (2) Saiyans

Reputation: 172
  • View blog
  • Posts: 955
  • Joined: 25-October 09

Re: Extracting a folder icon

Posted 06 March 2014 - 09:01 AM

I'm sure there are methods to get the icon of a directory, though I'm fairly sure you cannot do so in the same way you would a file.

Is it then not the specific icon that has been assigned to a folder you want, but the generic windows folder icon?
Posted Image
On the machine itself the icons are stored in shell32.dll , though I'm unsure as to how you would access them through VB.net . There are programs available which you can use to extract all the icons, which you could then use like any other image. Example

A google search revealed the below code to me, and given a casual look-over may work. Though for what you apparently need it may be simpler just to use a generic folder image.
Spoiler


EDIT: I see I spent too long writing my reply, since my preferred answer has been echoed above :D

This post has been edited by belgarion262: 06 March 2014 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#7 Deftware  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 09-December 13

Re: Extracting a folder icon

Posted 06 March 2014 - 09:14 AM

View Postbelgarion262, on 06 March 2014 - 09:01 AM, said:

I'm sure there are methods to get the icon of a directory, though I'm fairly sure you cannot do so in the same way you would a file.

Is it then not the specific icon that has been assigned to a folder you want, but the generic windows folder icon?
Posted Image
On the machine itself the icons are stored in shell32.dll , though I'm unsure as to how you would access them through VB.net . There are programs available which you can use to extract all the icons, which you could then use like any other image. Example

A google search revealed the below code to me, and given a casual look-over may work. Though for what you apparently need it may be simpler just to use a generic folder image.
Spoiler


EDIT: I see I spent too long writing my reply, since my preferred answer has been echoed above :D/>


thank you so much for the extractor program link!!!
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1416
  • View blog
  • Posts: 3,627
  • Joined: 01-February 13

Re: Extracting a folder icon

Posted 06 March 2014 - 08:14 PM

Hi,
You can use the SHGetFileInfo API function to extract the icons of Folders, Files, or Disk Drives.

I set up a Function in this example that you can call with the pathname of the Drive, Folder, or File that you want the icon from. You also need to specify True to get the Large (32x32) icon or False to get the small (16x16) icon.

If you want a files icon you must use the full path and name incuding the extension like "C:\MyFolder\MyFile.ext".

If you want a Disk Drive icon you must include the backslash on its name like "C:\" or "E:\".

If you want a folders icon you just use the full path with no backslash like "C:\Folder1\MyFolder"
Imports System.Runtime.InteropServices

Public Class Form1
    Private Const SHGFI_ICON As Integer = &H100
    Private Const SHGFI_LARGEICON As Integer = &H0
    Private Const SHGFI_SMALLICON As Integer = &H1

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.[Unicode])> _
    Public Structure SHFILEINFOW
        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", EntryPoint:="SHGetFileInfoW")> _
    Private Shared Function SHGetFileInfoW(<InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFOW, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
    End Function

    <DllImport("user32.dll", EntryPoint:="DestroyIcon")> _
    Private Shared Function DestroyIcon(ByVal hIcon As System.IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Folder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) 'Gets the path to My Pictures folder
        PictureBox1.Image = GetIcon(Folder, False) 'Gets the small icon
        PictureBox2.Image = GetIcon(Folder, True) 'Gets the large icon
    End Sub

    Private Function GetIcon(ByVal PathName As String, ByVal LargeIco As Boolean) As Bitmap
        Dim fi As New SHFILEINFOW
        fi = New SHFILEINFOW
        If LargeIco Then
            SHGetFileInfoW(PathName, 0, fi, Marshal.SizeOf(fi), SHGFI_ICON Or SHGFI_LARGEICON)
        Else
            SHGetFileInfoW(PathName, 0, fi, Marshal.SizeOf(fi), SHGFI_ICON Or SHGFI_SMALLICON)
        End If
        Dim bm As Bitmap = Icon.FromHandle(fi.hIcon).ToBitmap
        DestroyIcon(fi.hIcon)
        Return bm
    End Function
End Class



Attached Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1