extracting system icons..

hi guyzz..
i am able to extract icons from any .dll file or .exe file..
but they are extracted as .bmp files n after i give them .ico extension they don't act as icon files.
is there any way to extract these icons from files like shell32.dll or explorer.exe
in their original .ico format..
ashtified_85
[324 byte] By [AshishYadav] at [2007-12-16]
# 1
I use the ExtractIconEx API function and it saves the files as icons, the only thing is that icons are 16 color .Sad
Eisa at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
hi eisa,
thanx for replying
well i too did the same thing first displayed it on a picture box using the icon handle n then i saved it with .ico extension ..
n even then it is showing it as icon file..
but wen i tried to change one icon with that it said..
"icon fille missing"
so can u elaborate more or any code snippet would be good..
ashtified_85
AshishYadav at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Use the Icon class and it's FromHandle and Save methods.
TaDa at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
hi TaDa ,
thanx for ur reply.
well i tried this method but getting problems in save method in which i have to give system.io.stream as a output stream.which stream i should choose to save bitmap file syncrously on the hard disk..
any code would be good
ashtified_85
AshishYadav at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Did you already get this one answered.
WALLACETL1 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6


I hope this helps. This is a properties panel that reliably extracts all icons from an image and stores them in an imagelist. They could be written out in the format of your choice after they are extracted.
A major secret of this is to set the color depth of the image list control to 32 bits.
Imports
System.Object

Public Class FrmChangeIcon

Protected LargeImagelist As New ImageList

Protected bItemSelected As Boolean

Protected SelectedImage As Image

Protected Button As ToolStripButton = FrmToolBar.GetSelectedToolStripButton()

Protected SelectedPacket As clsIO.psec = FrmToolBar.GetSelectedToolStripButtonPacket

Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconExA" _

(ByVal lpszFile As String, ByVal nIconIndex As Integer, _

ByRef phiconLarge As Integer, ByRef phiconSmall As Integer, _

ByVal nIcons As Long) As Integer

'calls API to determine how many icons are embedded in the file

'vPath can contain an index number. It must be separated by a comma

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

LargeImagelist.ColorDepth = ColorDepth.Depth32Bit

LargeImagelist.ImageSize = New Size(32, 32)

Lv.LargeImageList = LargeImagelist

DriveIconSearch(SelectedPacket.sFileSpec)

End Sub

Protected Sub DriveIconSearch(ByVal Path As String)

Dim IconNumber As Integer

Dim index As String

Dim indexedpath As String

Dim MaxIcons As Integer = GetNumberOfIcons(Path) - 1

For IconNumber = 0 To MaxIcons

index = CStr(IconNumber)

indexedpath = Path & "," & index

LargeImagelist.Images.Add(GetIcon(indexedpath).ToBitmap())

Lv.Items.Add(IconNumber)

Lv.Items(IconNumber).ImageIndex = IconNumber

Next

End Sub

Public Sub HandFileSpecToIconForm(ByVal FileSpec As String)

tbFileName.Text = FileSpec

End Sub

Private Sub Lv_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles Lv.ItemSelectionChanged

bItemSelected = True

SelectedImage = LargeImagelist.Images(e.ItemIndex)

End Sub

Private Sub OK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckOK.Click

Me.Close()

End Sub

Private Function GetNumberOfIcons(ByVal vPath As String) As Integer

Dim strfile As String

If vPath.IndexOf(",") > 0 Then

strfile = vPath.Substring(0, vPath.IndexOf(","))

Else

strfile = vPath

End If

strfile = strfile.Replace("""", "")

Return ExtractIcon(strfile, -1, 0, 0, 0)

End Function

'Loads Icon from any file.

'vPath can contain an index number. It must be separated by a comma

Private Function GetIcon(ByVal vPath As String) As Icon

Dim strfile As String

Dim intIndex, intIcon, intIcon2 As Integer

'Split path in index and path

If vPath.IndexOf(",") > 0 Then

strfile = vPath.Substring(0, vPath.IndexOf(","))

intIndex = Convert.ToInt32(vPath.Substring(vPath.IndexOf(",") + 1))

Else

strfile = vPath

intIndex = 0

End If

'If path contains dubblequotes remove them

strfile = strfile.Replace("""", "")

'Open the file

Dim fi As System.IO.FileInfo

Try

fi = New System.IO.FileInfo(strfile)

Catch ex As Exception

Console.WriteLine("Bad filename: " & strfile)

Console.WriteLine(ex.ToString)

Return Nothing

End Try

'Try To extract Icon at index

ExtractIcon(strfile, intIndex, intIcon, intIcon2, 1)

'If this failes take the first icon

If intIcon = 0 Then

ExtractIcon(strfile, 0, intIcon, intIcon2, 1)

End If

If intIcon <> 0 Then

Dim objIcon As Icon

Dim stream As New IO.MemoryStream

objIcon = GetIconFH(New IntPtr(intIcon))

Application.DoEvents()

objIcon.Save(stream)

Dim intLength As Integer = CType(stream.Length, Integer)

Dim b(intLength) As Byte

stream.Position = 0

stream.Read(b, 0, intLength)

stream.Close()

'DestroyIcon(intIcon)

Return DirectCast(objIcon.Clone, Icon)
Else

Return Nothing

End If

End Function

Private Shared Function GetIconFH(ByVal inticon As IntPtr) As Icon

GetIconFH = Icon.FromHandle(inticon)

End Function

Private Sub cbCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbCancel.Click

Me.Close()

End Sub

Private Sub cbBrowse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbBrowse.Click

Dim OpnFD As New OpenFileDialog()

OpnFD.Title = "Select an icon file"

OpnFD.Filter = "Executable files (*.exe;*.dll;*.ocx;*.ico)|*.exe;*.dll;*.ocx;*.ico"

OpnFD.InitialDirectory = Environ("HOMEDRIVE")

OpnFD.FilterIndex = 1

OpnFD.RestoreDirectory = True

OpnFD.ShowDialog()

If OpnFD.ShowDialog() = DialogResult.OK Then

Lv.Items.Clear()

LargeImagelist.Images.Clear()

tbFileName.Text = OpnFD.FileName

DriveIconSearch(OpnFD.FileName)

End If

End Sub

End Class

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

Hi,

I not only want to get the icons of EXE's but also of relatet files, directories and url's! E.g. for a .doc - File I need the related Word-Icon (or whatever is in use on the system).

Anyone has any Ideas?

Thanks in advance

Klaus

Klaus65 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...