Files
Pilz/Pilz.Win32/Internals/IconExtractor.vb
2019-12-10 15:13:07 +01:00

39 lines
1.4 KiB
VB.net

Imports System.Drawing
Imports System.Runtime.InteropServices
Imports Pilz.Win32.Native
Namespace Internals
Public Class IconExtractor
''' <summary>
''' Extrahiert das Icon aus einer Datei oder aus einem Ordner.
''' </summary>
''' <param name="FilePath">Hier übergeben Sie den Pfad der Datei von dem das Icon extrahiert werden soll.</param>
''' <param name="Small">Bei übergabe von true wird ein kleines und bei false ein großes Icon zurück gegeben.</param>
Public Shared Function ExtractIcon(ByVal FilePath As String, ByVal Small As Boolean) As Icon
Dim hImgSmall As IntPtr
Dim hImgLarge As IntPtr
Dim shinfo As New SHFILEINFO
If Small Then
hImgSmall = LibShell32.SHGetFileInfo(FilePath, 0, shinfo, CUInt(Math.Truncate(Marshal.SizeOf(shinfo))), SHFILEINFO.SHGFI_ICON Or SHFILEINFO.SHGFI_SMALLICON)
Else
hImgLarge = LibShell32.SHGetFileInfo(FilePath, 0, shinfo, CUInt(Math.Truncate(Marshal.SizeOf(shinfo))), SHFILEINFO.SHGFI_ICON Or SHFILEINFO.SHGFI_LARGEICON)
End If
Dim icon As Icon
Try
icon = Icon.FromHandle(shinfo.hIcon)
Catch ex As Exception
icon = Nothing
End Try
Return icon
End Function
End Class
End Namespace