Skip to content

Commit

Permalink
feat: v11.3.13 image
Browse files Browse the repository at this point in the history
  • Loading branch information
minkostaev committed Jan 9, 2025
1 parent ff911d0 commit db1f4bb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 36 deletions.
32 changes: 4 additions & 28 deletions ShortcutsGrid/Services/Image/ExtractIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,76 +40,52 @@ public static Icon ExtractIconFromExecutable(string path)
{
IntPtr hModule = LoadLibraryEx(path, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
var tmpData = new List<byte[]>();

ENUMRESNAMEPROC callback = (h, t, name, l) =>
{
var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name);

// Calculate the size of an entire .icon file.

int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount
int len = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
for (int i = 0; i < count; ++i)
len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes

len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes
using (var dst = new BinaryWriter(new MemoryStream(len)))
{
// Copy GRPICONDIR to ICONDIR.

dst.Write(dir, 0, 6);

int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count

for (int i = 0; i < count; ++i)
{
// Load the picture.

ushort id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID
ushort id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID
var pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id);

// Copy GRPICONDIRENTRY to ICONDIRENTRY.

dst.Seek(6 + 16 * i, 0);

dst.Write(dir, 6 + 14 * i, 8); // First 8bytes are identical.
dst.Write(pic.Length); // ICONDIRENTRY.dwBytesInRes
dst.Write(picOffset); // ICONDIRENTRY.dwImageOffset

// Copy a picture.

dst.Seek(picOffset, 0);
dst.Write(pic, 0, pic.Length);

picOffset += pic.Length;
}

tmpData.Add(((MemoryStream)dst.BaseStream).ToArray());
}
return true;
};
EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);
byte[][] iconData = tmpData.ToArray();
using (var ms = new MemoryStream(iconData[0]))
{
return new Icon(ms);
}
byte[][] iconData = [.. tmpData];
using (var ms = new MemoryStream(iconData[0])) { return new Icon(ms); }
}

private static byte[] GetDataFromResource(IntPtr hModule, IntPtr type, IntPtr name)
{
// Load the binary data from the specified resource.

IntPtr hResInfo = FindResource(hModule, name, type);

IntPtr hResData = LoadResource(hModule, hResInfo);

IntPtr pResData = LockResource(hResData);

uint size = SizeofResource(hModule, hResInfo);

byte[] buf = new byte[size];
Marshal.Copy(pResData, buf, 0, buf.Length);

return buf;
}

Expand Down
3 changes: 0 additions & 3 deletions ShortcutsGrid/Services/Image/IconExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ public static ImageSource ToImageSource(this Icon icon)
{
Bitmap bitmap = icon.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap();

ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());

if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
}

return wpfBitmap;
}

Expand Down
2 changes: 1 addition & 1 deletion ShortcutsGrid/Services/Image/ImageUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static ImageSource Base64StringToImage(string base64ImageString)
{
if (imgPath.IsBase64())
{
return ImageUtilities.Base64StringToImage(imgPath);
return Base64StringToImage(imgPath);
}
else if (File.Exists(imgPath))
{
Expand Down
2 changes: 1 addition & 1 deletion ShortcutsGrid/Services/ReadShortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static List<Shortcut> JsonToShortcuts(string? filePath)
return shortcuts;

string json = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<List<Shortcut>>(json) ?? new List<Shortcut>();
return JsonSerializer.Deserialize<List<Shortcut>>(json) ?? [];
}

}
6 changes: 3 additions & 3 deletions ShortcutsGrid/ShortcutsGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<UseWPF>true</UseWPF>
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
<ApplicationIcon>Resources\web-browser-128.ico</ApplicationIcon>
<AssemblyVersion>1.3.12.0</AssemblyVersion>
<FileVersion>1.3.12.0</FileVersion>
<VersionPrefix>1.3.12</VersionPrefix>
<AssemblyVersion>1.3.13.0</AssemblyVersion>
<FileVersion>1.3.13.0</FileVersion>
<VersionPrefix>1.3.13</VersionPrefix>
<VersionSuffix>$([System.DateTime]::UtcNow.ToString(yyyy-MM-dd))</VersionSuffix>
</PropertyGroup>

Expand Down

0 comments on commit db1f4bb

Please sign in to comment.