Grabbing another applications icon [solved]

Posts 1–4 of 4 · Page 1 of 1
Grabbing another applications icon [solved]
Well basically, I need to grab the icon of any file opened with OpenFileDialog. It will then be added as a picture on a button.

The application is a "Game Starter" so when you click the buttons you will start the game. The icon has to be on the button or you won't be able to tell what game you're choosing.

I just need to know some way to grab the icon of the application selected.

Basically just "GetIcon(ofdSelectGame1.Filename)".

You can do it with the "SHGetFileInfo" API in shell32:

Code:
    <System.Runtime.InteropServices.DllImport("shell32.dll")> _
    Private Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As UInt32, ByRef structPtr As IntPtr, ByVal cbFileInfo As UInt32, ByVal uFlagsn As UInt32) As Int32
    End Function

    Public Shared Function ExtractIcon(ByVal path As String, ByVal large As Boolean) As Icon
        Dim hwnd As IntPtr = IntPtr.Zero
        Dim flags As UInt32 = &H100 Or &H10 Or CUInt(If(large, &H0, &H1))
        SHGetFileInfo(path, 256, hwnd, 0, flags)
        If hwnd <> IntPtr.Zero Then
            Return Icon.FromHandle(hwnd)
        Else
            Return Nothing
        End If
    End Function
It's probably a dodgy API call there actually, I'd recommend going here for better examples. I just wrote it out in a hurry.

Code:
Me.PictureBox1.Image = ExtractIcon("C:\example.exe", true).ToBitmap()
Quote Originally Posted by Jason View Post
You can do it with the "SHGetFileInfo" API in shell32:

Code:
    <System.Runtime.InteropServices.DllImport("shell32.dll")> _
    Private Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As UInt32, ByRef structPtr As IntPtr, ByVal cbFileInfo As UInt32, ByVal uFlagsn As UInt32) As Int32
    End Function

    Public Shared Function ExtractIcon(ByVal path As String, ByVal large As Boolean) As Icon
        Dim hwnd As IntPtr = IntPtr.Zero
        Dim flags As UInt32 = &H100 Or &H10 Or CUInt(If(large, &H0, &H1))
        SHGetFileInfo(path, 256, hwnd, 0, flags)
        If hwnd <> IntPtr.Zero Then
            Return Icon.FromHandle(hwnd)
        Else
            Return Nothing
        End If
    End Function
It's probably a dodgy API call there actually, I'd recommend going here for better examples. I just wrote it out in a hurry.

Code:
Me.PictureBox1.Image = ExtractIcon("C:\example.exe", true).ToBitmap()
Tommy helped me figure it out, well, he did everything but still.
Solved.

@Jason

Code:
Private Sub btnGame1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGame1.Click
        If ofdSelectGame1.FileName = "" Then
            ofdSelectGame1.ShowDialog()
            Dim ico As Icon = Icon.ExtractAssociatedIcon(ofdSelectGame1.FileName)
            btnGame1.Image = ico.ToBitmap()
        Else
            Shell(ofdSelectGame1.FileName)
        End If
    End Sub
Wow, that's a lot easier rofl.

Marked solved, always pays to read up the documentation of classes I guess .
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us