Ive made an icon library for my program, so that I dont have my program depend on Windows for icons. I want to know how I can have my program use the icons from that dll file instead of from individual icons that I have in the program directory.

This code should do it:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Namespace ExtractIcon
    Partial Public Class Form1
        Inherits Form
        ' Constants that we need in the function call
        Private Const SHGFI_ICON As Integer = &H100
        Private Const SHGFI_SMALLICON As Integer = &H1
        Private Const SHGFI_LARGEICON As Integer = &H0

        ' This structure will contain information about the file
        Public Structure SHFILEINFO
            ' Handle to the icon representing the file
            Public hIcon As IntPtr
            ' Index of the icon within the image list
            Public iIcon As Integer
            ' Various attributes of the file
            Public dwAttributes As UInteger
            ' Path to the file
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
            Public szDisplayName As String
            ' File type
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
            Public szTypeName As String
        End Structure
        ' The signature of SHGetFileInfo (located in Shell32.dll)

        <DllImport("Shell32.dll")> _
        Public Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As UInteger) As IntPtr
        End Function

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub btnExtract_Click(ByVal sender As Object, ByVal e As EventArgs)
            ' Will store a handle to the small icon
            Dim hImgSmall As IntPtr
            ' Will store a handle to the large icon
            Dim hImgLarge As IntPtr
            Dim shinfo As New SHFILEINFO()
            ' Open the file that we wish to extract the icon from
            If openFile.ShowDialog() = DialogResult.OK Then
                ' Store the file name
                Dim FileName As String = openFile.FileName
                ' Store the icon in this myIcon object
                Dim myIcon As System.Drawing.Icon
                ' Get a handle to the small icon
                hImgSmall = SHGetFileInfo(FileName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
                ' Get the small icon from the handle
                myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
                ' Display the small icon
                picIconSmall.Image = myIcon.ToBitmap()
                ' Get a handle to the large icon
                hImgLarge = SHGetFileInfo(FileName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
                ' Get the large icon from the handle
                myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
                ' Display the large icon
                picIconLarge.Image = myIcon.ToBitmap()
            End If
        End Sub
    End Class
End Namespace

Would that be able to work with my custom icon library, rather than the Windows Icon library? The icons Im using are only found in Vista, and I want this to work in any OS.

Also, Im getting errors that OpenFile.Filename is not available. And the picIcons are not declared? I dont even know why that is giving me an error.

Also, Im getting errors that OpenFile.Filename is not available. And the picIcons

The code doesn't work "as is". You have to modify it for your purposes like you do with any other sample code. Just drop OpenFileDialog and Picture Box controls on the form.

I didn't test djjaries's code but I've used the same API call to extract icons. It works at least with XP and Vista. Never tested or checked that API call with any pre-XP OS or with Win7 RC1. Instead of API call there's also a .NET way to do it.

Ive made an icon library for my program

That DLL is called a satellite assembly and you access it with ResourceManager class. Satellite assembly and how to use it is explained very well (with sample code) in MSDN .NET framework developer's guide article Retrieving Resources in Satellite Assemblies.

Ive manipulated the code to get it to work, but sort of. I still need it to use the icon I tell it, as the icon of the program, and not just display the icon. Also, its not supposed to be a user selected icon, this has to be hard coded.

I guess you didn't use ResourseManager.

its not supposed to be a user selected icon, this has to be hard coded.

Assuming your assembly ends up to same directory as the main executable, you get the path to your dll in this way

Dim StartUpFolder As String
' Get the path where your app starts
StartUpFolder = Application.StartupPath
Dim MyDLLFile As String
' Append your dll's name to path
MyDLLFile = StartUpFolder & "\myicondll.DLL"

I still need it to use the icon I tell it, as the icon of the program

Once you've managed to load the right icon from the dll, assign it to (main) form's icon property

Dim myIcon As System.Drawing.Icon
' DEBUG: Load from the file
myIcon = New Icon("D:\icon.ico")

' Set the icon as the app's (i.e. main form's) icon
Me.Icon = myIcon

HTH

still didnt work after all that, it wont load the icon from the dll, and gives me an error. I'll try to mess around some more.

The icons Im using are only found in Vista, and I want this to work in any OS.

You did write your own icon library, right? Are you sure your DLL contains valid resources i.e. ico-files?

I still suggest taking a look at Retrieving Resources in Satellite Assemblies. With ResourceManager class you should have a code that works in any platform that supports .NET Framework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.