问题描述
我从.exe / .dll解压缩图标,并想将其保存在.ico文件中。
我试图使用 :: OleCreatePictureIndirect()
然后 code> IPicture-> SaveAsFile()。它工作,但图标的透明部分涂黑色(显然不是透明的任何更多:()。 我尝试手动解析它工作正常,但是麻烦,恐怕与Vista图标/ .icl文件/ etc的并发症。
请帮助谢谢。
您可以用 IPicture :: SaveAsFile()
保存 HICON
下面是一个使用它的示例C ++程序:
#includestdafx.h
#include< windows.h>
#include< olectl.h>
#pragma comment(lib,oleaut32.lib)
HRESULT SaveIcon(HICON hIcon,const wchar_t * path ){
//创建IPicture表面
PICTDESC desc = {sizeof(PICTDESC)};
desc.picType = PICTYPE_ICON;
desc.icon.hicon = hIcon;
IPicture * pPicture = 0;
HRESULT hr = OleCreatePictureIndirect(& desc,IID_IPicture,FALSE,(void **)& pPicture);
if(FAILED(hr))return hr;
//创建流并保存图像
IStream * pStream = 0;
CreateStreamOnHGlobal(0,TRUE,& pStream);
LONG cbSize = 0;
hr = pPicture-> SaveAsFile(pStream,TRUE,& cbSize);
//将流内容写入文件
if(!FAILED(hr)){
HGLOBAL hBuf = 0;
GetHGlobalFromStream(pStream,& hBuf);
void * buffer = GlobalLock(hBuf);
HANDLE hFile = CreateFile(path,GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0);
if(!hFile)hr = HRESULT_FROM_WIN32(GetLastError());
else {
DWORD written = 0;
WriteFile(hFile,buffer,cbSize,& written,0);
CloseHandle(hFile);
}
GlobalUnlock(buffer);
}
//清理
pStream-> Release();
pPicture-> Release();
return hr;
}
int _tmain(int argc,_TCHAR * argv [])
{
HICON hIcon =(HICON)LoadImage(0,Lc:\ \windows\\system32\\perfcentercpl.ico,IMAGE_ICON,32,32,LR_LOADFROMFILE);
if(!hIcon)return GetLastError();
HRESULT hr = SaveIcon(hIcon,Lc:\\temp\\ test.ico);
return hr;
}
I am extracting an icon from .exe/.dll and want to save it in an .ico file. What is the best way to do this?
I have tried to use ::OleCreatePictureIndirect()
and then IPicture->SaveAsFile()
. It works but transparent parts of the icon are painted black (and obviously are not transparent any more :( ).
I tried manual parsing. It works OK but is cumbersome and I am afraid of complications with Vista icons/.icl files/etc.
Please, help. Thanks.
You can save HICON
s with the IPicture::SaveAsFile()
method. Here's a sample C++ program that uses it:
#include "stdafx.h"
#include <windows.h>
#include <olectl.h>
#pragma comment(lib, "oleaut32.lib")
HRESULT SaveIcon(HICON hIcon, const wchar_t* path) {
// Create the IPicture intrface
PICTDESC desc = { sizeof(PICTDESC) };
desc.picType = PICTYPE_ICON;
desc.icon.hicon = hIcon;
IPicture* pPicture = 0;
HRESULT hr = OleCreatePictureIndirect(&desc, IID_IPicture, FALSE, (void**)&pPicture);
if (FAILED(hr)) return hr;
// Create a stream and save the image
IStream* pStream = 0;
CreateStreamOnHGlobal(0, TRUE, &pStream);
LONG cbSize = 0;
hr = pPicture->SaveAsFile(pStream, TRUE, &cbSize);
// Write the stream content to the file
if (!FAILED(hr)) {
HGLOBAL hBuf = 0;
GetHGlobalFromStream(pStream, &hBuf);
void* buffer = GlobalLock(hBuf);
HANDLE hFile = CreateFile(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (!hFile) hr = HRESULT_FROM_WIN32(GetLastError());
else {
DWORD written = 0;
WriteFile(hFile, buffer, cbSize, &written, 0);
CloseHandle(hFile);
}
GlobalUnlock(buffer);
}
// Cleanup
pStream->Release();
pPicture->Release();
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HICON hIcon = (HICON)LoadImage(0, L"c:\\windows\\system32\\perfcentercpl.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if (!hIcon) return GetLastError();
HRESULT hr = SaveIcon(hIcon, L"c:\\temp\\test.ico");
return hr;
}
这篇关于如何将HICON保存到.ico文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!