migrate Pilz.Win32 & optimize Pilz & Pilz.IO
This commit is contained in:
36
Pilz.Win32/Internals/IconExtractor.cs
Normal file
36
Pilz.Win32/Internals/IconExtractor.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Pilz.Win32.Mapped;
|
||||
using Pilz.Win32.Native;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Pilz.Win32.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="size">Bei übergabe von true wird ein kleines und bei false ein großes Icon zurück gegeben.</param>
|
||||
public static Icon ExtractIcon(string FilePath, SystemIconSize size)
|
||||
{
|
||||
Icon icon;
|
||||
var shinfo = new SHFILEINFO();
|
||||
var small = size == SystemIconSize.Small;
|
||||
|
||||
Shell32.SHGetFileInfo(FilePath, 0U, ref shinfo, (uint)Math.Round(Math.Truncate((decimal)Marshal.SizeOf(shinfo))), SHFILEINFO.SHGFI_ICON | (small ? SHFILEINFO.SHGFI_SMALLICON : SHFILEINFO.SHGFI_LARGEICON));
|
||||
|
||||
try
|
||||
{
|
||||
icon = Icon.FromHandle(shinfo.hIcon);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
icon = null;
|
||||
}
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user