Repository navigation
Expand file tree
/
Copy pathIconExtractor.cs
More file actions
93 lines (84 loc) · 3.04 KB
/
Copy pathIconExtractor.cs
File metadata and controls
93 lines (84 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ida_picker
{
public static class IconExtractor
{
public static ImageSource ExtractIcon(string installPath)
{
try
{
string icoPath = Path.Combine(installPath, "ida.ico");
if (File.Exists(icoPath))
{
return new BitmapImage(new Uri(icoPath, UriKind.Absolute));
}
string ida64Path = Path.Combine(installPath, "ida64.exe");
if (File.Exists(ida64Path))
{
return ExtractIconFromExe(ida64Path);
}
string idaPath = Path.Combine(installPath, "ida.exe");
if (File.Exists(idaPath))
{
return ExtractIconFromExe(idaPath);
}
}
catch (Exception)
{
// Fall through to default icon
}
// Return a default icon if extraction fails
return CreateDefaultIcon();
}
private static ImageSource ExtractIconFromExe(string exePath)
{
try
{
using (Icon? icon = Icon.ExtractAssociatedIcon(exePath))
{
if (icon != null)
{
return Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}
}
catch (Exception)
{
// Fall through to default
}
return CreateDefaultIcon();
}
private static ImageSource CreateDefaultIcon()
{
// Create a simple default icon
var drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawRectangle(
System.Windows.Media.Brushes.LightGray,
new System.Windows.Media.Pen(System.Windows.Media.Brushes.Gray, 1),
new Rect(0, 0, 64, 64));
drawingContext.DrawText(
new FormattedText("IDA",
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface("Arial"),
20,
System.Windows.Media.Brushes.Black,
VisualTreeHelper.GetDpi(drawingVisual).PixelsPerDip),
new System.Windows.Point(12, 22));
}
var renderTargetBitmap = new RenderTargetBitmap(64, 64, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
return renderTargetBitmap;
}
}
}