-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIconExtractor.cpp
More file actions
166 lines (136 loc) · 5.4 KB
/
FileIconExtractor.cpp
File metadata and controls
166 lines (136 loc) · 5.4 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Hi there.
// This file is a regrettable amalgamation of ancient Windows APIs that, for better or worse, works.
// This code is terrible, but making it better is honestly not something I am interested in doing,
// since at the end of the day, it will still involve low-level Win32 nonsense anyway.
// You have been warned.
// -----
// Helpful: https://hotcashew.com/2013/10/extracting-icons/
#include <Windows.h>
#include <olectl.h>
#include <ShObjIdl.h> //For IShellItemImageFactory
#include <Shlwapi.h>
#include <stdio.h>
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "Shlwapi")
#pragma comment(lib, "Gdiplus")
void DisplayUsage()
{
wprintf(L"Usage:\n");
wprintf(L"ImageFactorySample.exe <size> <path-to-input> <path-to-output>\n");
wprintf(L"e.g. ImageFactorySample.exe 256 C:/some/path/program.exe C:/some/path/program.png\n");
}
// https://stackoverflow.com/a/24645420
#include <gdiplus.h>
using namespace Gdiplus;
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
// List all available encoders:
// for (UINT j = 0; j < num; ++j)
// wprintf(L"%ls\n", pImageCodecInfo[j].MimeType);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
bool WriteToImageFile(Bitmap* image, LPCWSTR filePath, LPCWSTR formatMimeType)
{
CLSID myClsId;
int retVal = GetEncoderClsid(formatMimeType, &myClsId);
if (retVal == -1) return false;
image->Save(filePath, &myClsId, nullptr);
return true;
}
int wmain(int argc, wchar_t *argv[])
{
if (argc != 4)
{
DisplayUsage();
}
else
{
int nSize = _wtoi(argv[1]);
if (!nSize)
{
DisplayUsage();
}
else
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
LPCWSTR pwszError{};
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
// Getting the IShellItemImageFactory interface pointer for the file.
IShellItemImageFactory *pImageFactory;
hr = SHCreateItemFromParsingName(argv[2], nullptr, IID_PPV_ARGS(&pImageFactory));
if (SUCCEEDED(hr))
{
SIZE size = { nSize, nSize };
HBITMAP hbitmap;
hr = pImageFactory->GetImage(size, SIIGBF_BIGGERSIZEOK|SIIGBF_ICONONLY|SIIGBF_SCALEUP, &hbitmap);
if (SUCCEEDED(hr))
{
// https://stackoverflow.com/questions/69344394/is-there-a-way-to-save-a-png-from-bitmap-with-transparency-gdi
//Creating nessecery palettes for gdi, so i can use Bitmap::FromHBITMAP
PALETTEENTRY pat = { 255,255,255,0 };
LOGPALETTE logpalt = { 0, 1, pat };
HPALETTE hpalette = CreatePalette(&logpalt);
//Getting data from the bitmap and creating new bitmap from it
auto bitmap = Bitmap::FromHBITMAP(hbitmap, hpalette);
Rect rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());
BitmapData bd;
bitmap->LockBits(&rect, ImageLockModeRead, bitmap->GetPixelFormat(), &bd);
Bitmap* bitmapWithAlpha = new Bitmap(bd.Width, bd.Height, bd.Stride, PixelFormat32bppARGB, (BYTE*)bd.Scan0);
bitmap->UnlockBits(&bd);
if (!WriteToImageFile(bitmapWithAlpha, argv[3], L"image/png"))
{
wprintf(L"Failed to write to file :(\n");
}
delete bitmapWithAlpha;
delete bitmap;
DeleteObject(hbitmap);
}
else
{
pwszError = L"IShellItemImageFactory::GetImage failed with error code %x";
}
pImageFactory->Release();
}
else
{
pwszError = L"SHCreateItemFromParsingName failed with error %x";
}
CoUninitialize();
}
else
{
pwszError = L"CoInitializeEx failed with error code %x";
}
if (FAILED(hr) && pwszError)
{
wprintf(pwszError, hr);
}
GdiplusShutdown(gdiplusToken);
}
}
return 0;
}