我正在尝试获取和图标从和exe,并保存为png与透明度。我已经能够用gdi+从其中获取位图,并将.bmp保存为正确的alpha通道(在Photoshop中选中)。但现在的问题是,当我想要将它保存为.png时,透明度不会传输到文件中(透明区域是黑色的)以下是我的代码:
using namespace Gdiplus;
SHFILEINFO sfi;
IImageList* piml;
HICON hicon;
// Getting the largest icon from FILEPATH(*.exe)
SHGetFileInfo(FILEPATH, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);
SHGetImageList(SHIL_JUMBO, IID_IImageList, (void**)&piml);
piml->GetIcon(sfi.iIcon, 0x00000001, &hicon);
// Getting the HBITMAP from it
ICONINFO iconinfo;
GetIconInfo(hicon, &iconinfo);
//GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//Creating nessecery palettes for gdi, so i can use Bitmap::FromHBITMAP
PALETTEENTRY pat = { 255,255,255,0 };
LOGPALETTE logpalt = { 0, 1, pat };
HPALETTE hand = CreatePalette(&logpalt);
Bitmap* h = Bitmap::FromHBITMAP(iconinfo.hbmColor, hand);
//Getting image encoders for saving
UINT num, sz;
GetImageEncodersSize(&num, &sz);
ImageCodecInfo* inf = new ImageCodecInfo[sz];
GetImageEncoders(num, sz, inf);
//4 -> png ; 0 -> bitmap
CLSID encoderClsid = inf[4].Clsid;
h->Save(L"PATH_TO_SAVE", &encoderClsid, NULL);
GdiplusShutdown(gdiplusToken);
发布于 2021-09-27 15:31:49
显然,alpha通道包含所有需要的数据,但由于某些原因,它在保存时忽略了alpha通道,修复方法是从以前的位图创建新的位图。
现在,生成的PNG具有透明性。
这是固定的代码。
using namespace Gdiplus;
SHFILEINFO sfi;
IImageList* piml;
HICON hicon;
// Getting the largest icon from FILEPATH(*.exe)
SHGetFileInfo(FILEPATH, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);
SHGetImageList(SHIL_JUMBO, IID_IImageList, (void**)&piml);
piml->GetIcon(sfi.iIcon, 0x00000001, &hicon);
// Getting the HBITMAP from it
ICONINFO iconinfo;
GetIconInfo(hicon, &iconinfo);
//GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//Creating nessecery palettes for gdi, so i can use Bitmap::FromHBITMAP
PALETTEENTRY pat = { 255,255,255,0 };
LOGPALETTE logpalt = { 0, 1, pat };
HPALETTE hand = CreatePalette(&logpalt);
Bitmap* h = Bitmap::FromHBITMAP(iconinfo.hbmColor, hand);
//Getting data from the bitmap and creating new bitmap from it
Rect rect(0, 0, h->GetWidth(), h->GetHeight());
BitmapData bd;
h->LockBits(&rect, ImageLockModeRead, h->GetPixelFormat(), &bd);
Bitmap* newBitmapWithAplha = new Bitmap(bd.Width, bd.Height, bd.Stride, PixelFormat32bppARGB, (BYTE*)bd.Scan0);
h->UnlockBits(&bd);
//Getting image encoders for saving
UINT num, sz;
GetImageEncodersSize(&num, &sz);
ImageCodecInfo* inf = new ImageCodecInfo[sz];
GetImageEncoders(num, sz, inf);
//4 -> png ; 0 -> bitmap
CLSID encoderClsid = inf[4].Clsid;
//Saving the new Bitmap
newBitmapWithAplha->Save(L"PATH_TO_SAVE", &encoderClsid, NULL);
GdiplusShutdown(gdiplusToken);
https://stackoverflow.com/questions/69344394
复制相似问题