| | |
Reading Icons from an Icon Library
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 6
Reputation:
Solved Threads: 2
This code should do it:
VB Syntax (Toggle Plain Text)
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
•
•
•
•
Also, Im getting errors that OpenFile.Filename is not available. And the picIcons
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
Teme64 @ Windows Developer Blog
I guess you didn't use ResourseManager.
Assuming your assembly ends up to same directory as the main executable, you get the path to your dll in this way
Once you've managed to load the right icon from the dll, assign it to (main) form's icon property
HTH
•
•
•
•
its not supposed to be a user selected icon, this has to be hard coded.
VB.NET Syntax (Toggle Plain Text)
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
VB.NET Syntax (Toggle Plain Text)
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
Teme64 @ Windows Developer Blog
•
•
•
•
The icons Im using are only found in Vista, and I want this to work in any OS.
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.
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Icons For Designers (Graphics and Multimedia)
- Need to Delete Icon From Taskbar "Past Items" (Windows NT / 2000 / XP)
- Open Source or Free Icon software to make ico Files (IT Professionals' Lounge)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- STL container-initialization question (C++)
- Disappearing T(ric)k. (Python)
- Desktop Icon (Windows NT / 2000 / XP)
- folder view (icon) (Windows NT / 2000 / XP)
- How do you apply/use the OSI MODEL? (Networking Hardware Configuration)
- Icons layered on top of original desktop icons (Viruses, Spyware and other Nasties)
Other Threads in the VB.NET Forum
- Previous Thread: export data from access to excel in .NET
- Next Thread: simple collision detection
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account advanced array basic beginner browser button buttons center check code combo crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade filter forms ftp generatetags gridview html images input insert intel internet listview map mobile monitor net number objects open panel passingparameters pdf picturebox picturebox2 port position print printing problem save searchbox searchvb.net select serial settings shutdown soap socket sqlserver survey table tcp temperature textbox timer timespan transparency trim txttoxmlconverter update user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





