Skip to content

Commit 1a1837d

Browse files
committed
fix: transparent bg and types
1 parent b1d0313 commit 1a1837d

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

index.d.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
declare module "winicon" {
2-
/**
3-
* Extracts the associated windows icon from a file and saves it as a PNG.
4-
* @param inputPath Absolute path to the file (e.g., `.exe`, `.lnk`)
5-
* @param outputPath Absolute path to save the PNG
6-
* @param size Size of the icon (e.g., 256)
7-
*/
8-
export function getIcon(inputPath: string, outputPath: string, size: number): void;
1+
/**
2+
* Extracts the associated windows icon from a file and saves it as a PNG.
3+
* @param inputPath Absolute path to the file (e.g., `.exe`, `.lnk`)
4+
* @param outputPath Absolute path to save the PNG
5+
* @param size Size of the icon (e.g., 256)
6+
*/
7+
export function getIcon(inputPath: string, outputPath: string, size: number): void;
98

10-
/**
11-
* Extracts a thumbnail provided by applications using IThumbnailProvider and saves it as a PNG.
12-
* @param inputPath Absolute path to the file (e.g., `.pdf`, `.docx`)
13-
* @param outputPath Absolute path to save the PNG
14-
* @param size Size of the thumbnail
15-
*/
16-
export function getThumbnail(inputPath: string, outputPath: string, size: number): void;
17-
}
9+
/**
10+
* Extracts a thumbnail provided by applications using IThumbnailProvider and saves it as a PNG.
11+
* @param inputPath Absolute path to the file (e.g., `.pdf`, `.docx`)
12+
* @param outputPath Absolute path to save the PNG
13+
* @param size Size of the thumbnail
14+
*/
15+
export function getThumbnail(inputPath: string, outputPath: string, size: number): void;

src/winicon.cc

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ HRESULT GetIconImage(const std::wstring& filePath, int size, HBITMAP& hBitmap) {
3535
}
3636

3737
SIZE sizeStruct = { size, size };
38-
hr = pImageFactory->GetImage(sizeStruct, SIIGBF_BIGGERSIZEOK | SIIGBF_ICONONLY, &hBitmap);
38+
hr = pImageFactory->GetImage(sizeStruct, SIIGBF_BIGGERSIZEOK | SIIGBF_ICONONLY | SIIGBF_RESIZETOFIT, &hBitmap);
3939

4040
pImageFactory->Release();
4141
pShellItem->Release();
@@ -63,13 +63,34 @@ HRESULT GetThumbnailImage(const std::wstring& filePath, int size, HBITMAP& hBitm
6363
}
6464

6565
void SaveBitmapAsPNG(HBITMAP hBitmap, const std::wstring& outputPath) {
66-
Bitmap* bmp = Bitmap::FromHBITMAP(hBitmap, nullptr);
67-
if (!bmp) return;
66+
GdiplusStartupInput gdiplusStartupInput;
67+
ULONG_PTR gdiplusToken;
68+
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
69+
70+
BITMAP bm;
71+
GetObject(hBitmap, sizeof(BITMAP), &bm);
72+
73+
Bitmap bitmap(bm.bmWidth, bm.bmHeight, PixelFormat32bppARGB);
74+
75+
BitmapData bmpData;
76+
Rect rect(0, 0, bm.bmWidth, bm.bmHeight);
77+
78+
if (bitmap.LockBits(&rect, ImageLockModeWrite, PixelFormat32bppARGB, &bmpData) == Ok) {
79+
int bytesPerPixel = 4;
80+
BYTE* destData = static_cast<BYTE*>(bmpData.Scan0);
81+
82+
HDC hdc = GetDC(nullptr);
83+
GetDIBits(hdc, hBitmap, 0, bm.bmHeight, destData, (BITMAPINFO*)&bm, DIB_RGB_COLORS);
84+
ReleaseDC(nullptr, hdc);
85+
86+
bitmap.UnlockBits(&bmpData);
87+
}
6888

6989
CLSID clsidPng;
7090
CLSIDFromString(L"{557CF406-1A04-11D3-9A73-0000F81EF32E}", &clsidPng);
71-
bmp->Save(outputPath.c_str(), &clsidPng, nullptr);
72-
delete bmp;
91+
bitmap.Save(outputPath.c_str(), &clsidPng, nullptr);
92+
93+
GdiplusShutdown(gdiplusToken);
7394
}
7495

7596
void getImage(const Napi::CallbackInfo& info, bool useThumbnail) {
@@ -138,4 +159,4 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
138159

139160
NODE_API_MODULE(winicon, Init)
140161

141-
#endif
162+
#endif

0 commit comments

Comments
 (0)