Reading Icons from an Icon Library

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

Reading Icons from an Icon Library

 
0
  #1
Jul 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: djjaries is an unknown quantity at this point 
Solved Threads: 2
djjaries djjaries is offline Offline
Newbie Poster

Re: Reading Icons from an Icon Library

 
0
  #2
Jul 23rd, 2009
This code should do it:

  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Text
  7. Imports System.Windows.Forms
  8. Imports System.Runtime.InteropServices
  9.  
  10. Namespace ExtractIcon
  11. Partial Public Class Form1
  12. Inherits Form
  13. ' Constants that we need in the function call
  14. Private Const SHGFI_ICON As Integer = &H100
  15. Private Const SHGFI_SMALLICON As Integer = &H1
  16. Private Const SHGFI_LARGEICON As Integer = &H0
  17.  
  18. ' This structure will contain information about the file
  19. Public Structure SHFILEINFO
  20. ' Handle to the icon representing the file
  21. Public hIcon As IntPtr
  22. ' Index of the icon within the image list
  23. Public iIcon As Integer
  24. ' Various attributes of the file
  25. Public dwAttributes As UInteger
  26. ' Path to the file
  27. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
  28. Public szDisplayName As String
  29. ' File type
  30. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
  31. Public szTypeName As String
  32. End Structure
  33. ' The signature of SHGetFileInfo (located in Shell32.dll)
  34.  
  35. <DllImport("Shell32.dll")> _
  36. 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
  37. End Function
  38.  
  39. Public Sub New()
  40. InitializeComponent()
  41. End Sub
  42.  
  43. Private Sub btnExtract_Click(ByVal sender As Object, ByVal e As EventArgs)
  44. ' Will store a handle to the small icon
  45. Dim hImgSmall As IntPtr
  46. ' Will store a handle to the large icon
  47. Dim hImgLarge As IntPtr
  48. Dim shinfo As New SHFILEINFO()
  49. ' Open the file that we wish to extract the icon from
  50. If openFile.ShowDialog() = DialogResult.OK Then
  51. ' Store the file name
  52. Dim FileName As String = openFile.FileName
  53. ' Store the icon in this myIcon object
  54. Dim myIcon As System.Drawing.Icon
  55. ' Get a handle to the small icon
  56. hImgSmall = SHGetFileInfo(FileName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
  57. ' Get the small icon from the handle
  58. myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
  59. ' Display the small icon
  60. picIconSmall.Image = myIcon.ToBitmap()
  61. ' Get a handle to the large icon
  62. hImgLarge = SHGetFileInfo(FileName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
  63. ' Get the large icon from the handle
  64. myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
  65. ' Display the large icon
  66. picIconLarge.Image = myIcon.ToBitmap()
  67. End If
  68. End Sub
  69. End Class
  70. End Namespace
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

Re: Reading Icons from an Icon Library

 
0
  #3
Jul 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

Re: Reading Icons from an Icon Library

 
0
  #4
Jul 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Reading Icons from an Icon Library

 
0
  #5
Jul 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

Re: Reading Icons from an Icon Library

 
0
  #6
Jul 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Reading Icons from an Icon Library

 
0
  #7
Jul 25th, 2009
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
  1. Dim StartUpFolder As String
  2. ' Get the path where your app starts
  3. StartUpFolder = Application.StartupPath
  4. Dim MyDLLFile As String
  5. ' Append your dll's name to path
  6. 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
  1. Dim myIcon As System.Drawing.Icon
  2. ' DEBUG: Load from the file
  3. myIcon = New Icon("D:\icon.ico")
  4.  
  5. ' Set the icon as the app's (i.e. main form's) icon
  6. Me.Icon = myIcon

HTH
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: Tamir09 is an unknown quantity at this point 
Solved Threads: 0
Tamir09 Tamir09 is offline Offline
Light Poster

Re: Reading Icons from an Icon Library

 
0
  #8
Aug 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Reading Icons from an Icon Library

 
0
  #9
Aug 5th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC