@@ -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
6565void 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
7596void getImage (const Napi::CallbackInfo& info, bool useThumbnail) {
@@ -138,4 +159,4 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
138159
139160NODE_API_MODULE (winicon, Init)
140161
141- #endif
162+ #endif
0 commit comments