2009-11-30 206 views
2

我正在使用IShellItemImageFactory来提取文件的图标。我能够成功地提取它并使用 SendDlgItemMessage(hDlg,IDC_STATIC2,STM_SETIMAGE,IMAGE_ICON,(LPARAM)hicon)在对话框中显示它。将HICON保存为PNG

看到输出:click here

的问题是,当我使用GDI保存此作为一个文件(PNG格式)+梯度不保留纠正。找到我正在使用的代码。

 
GdiplusStartupInput gdiplusStartupInput; 
ULONG_PTR gdiplusToken; 
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 
Bitmap *h = new Bitmap(256, 256, PixelFormat32bppARGB); 
Graphics* g = Graphics::FromImage(copy); 
HDC copyHdc = g->GetHDC(); 
DrawIconEx(copyHdc, 0, 0, hicon, 256, 256, 0, NULL, DI_NORMAL); 
g->ReleaseHDC(copyHdc);; 
CLSID encoderClsid; 
GetEncoderClsid(L"image/png", &encoderClsid); 
h->Save(L"D:\\mynew.png", &encoderClsid, NULL); 
GdiplusShutdown(gdiplusToken); 

,我的文本文件的提取后得到的输出:click here

谁能帮我解决这个?

问候, 马诺

+0

无法看到在链接的图像的任何差异。 – 2009-11-30 10:30:12

+0

看看两幅图像的渐变,第二幅获得了一些黑色。 – Manoj 2009-11-30 10:38:19

回答

2

我错过了从代码中“复制”:

Graphics* g = Graphics::FromImage(copy); 

望着,似乎在设备上下文你绘制图标,没有按”图像没有32位颜色(缺少Alpha通道)。

尝试创建DC这样的:

HDC hDC = CreateCompatibleDC(NULL); 

,然后选择一个32位的彩色(空)的位图成直流。之后,您可以绘制图标并保存。

+0

是的,它应该是Graphics * g = Graphics :: FromImage(h); – Manoj 2009-12-01 04:32:48

+0

好的,谢谢。我实现了这样的示例程序。 – Manoj 2009-12-01 06:40:49

6

本主题相当老,但我只是有同样的问题,花了很多小时找到解决方案,保留在PNG文件中的透明度。

既然问题可以在Java迎刃而解......

sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file); 
    ImageIcon icon = new ImageIcon(sf.getIcon(true)); 
    FileOutputStream bos = new FileOutputStream("d:\\icons\\icon.png"); 
    ImageIO.write((BufferedImage)icon.getImage(), "PNG", bos); 

......我看了看一个JDK的源代码。在函数“Java_sun_awt_shell_Win32ShellFolder2_getIconBits”的文件“\ jdk \ src \ windows \ native \ sun \ windows \ ShellFolder2.cpp”中,我发现了我需要的有价值的提示。

该函数从HICON中检索颜色位图并调用GetDIBits来获取图像数据。绘制图标并不是必需的 - 透明性将会丢失。

非常感谢JDK开发人员。

这是我最终的代码:

static CLSID g_pngClsid = GUID_NULL; 

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx 
extern int GetEncoderClsid(const WCHAR* format, CLSID* pClsid); 

static HICON getShellIconByIndex(int shilsize, int iImage) 
{ 
    IImageListPtr spiml; 
    SHGetImageList(shilsize, IID_PPV_ARGS(&spiml)); 

    HICON hico; 
    spiml->GetIcon(iImage, ILD_TRANSPARENT, &hico); 
    return hico; 
} 

static HICON getShellIcon(int shilsize, const std::wstring& fname) { 
    UINT flags = SHGFI_SYSICONINDEX; 
    SHFILEINFO fi = {0}; 
    HICON hIcon = NULL; 

    if (SHGetFileInfo(fname.c_str(), 0, &fi, sizeof(fi), flags) != 0) { 
     hIcon = getShellIconByIndex(shilsize, fi.iIcon); 
    } 

    return hIcon; 
} 

struct BITMAP_AND_BYTES { 
    Gdiplus::Bitmap* bmp; 
    int32_t* bytes; 
}; 

static BITMAP_AND_BYTES createAlphaChannelBitmapFromIcon(HICON hIcon) { 

    // Get the icon info 
    ICONINFO iconInfo = {0}; 
    GetIconInfo(hIcon, &iconInfo); 

    // Get the screen DC 
    HDC dc = GetDC(NULL); 

    // Get icon size info 
    BITMAP bm = {0}; 
    GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm); 

    // Set up BITMAPINFO 
    BITMAPINFO bmi = {0}; 
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    bmi.bmiHeader.biWidth = bm.bmWidth; 
    bmi.bmiHeader.biHeight = -bm.bmHeight; 
    bmi.bmiHeader.biPlanes = 1; 
    bmi.bmiHeader.biBitCount = 32; 
    bmi.bmiHeader.biCompression = BI_RGB; 

    // Extract the color bitmap 
    int nBits = bm.bmWidth * bm.bmHeight; 
    int32_t* colorBits = new int32_t[nBits]; 
    GetDIBits(dc, iconInfo.hbmColor, 0, bm.bmHeight, colorBits, &bmi, DIB_RGB_COLORS); 

    // Check whether the color bitmap has an alpha channel. 
     // (On my Windows 7, all file icons I tried have an alpha channel.) 
    BOOL hasAlpha = FALSE; 
    for (int i = 0; i < nBits; i++) { 
     if ((colorBits[i] & 0xff000000) != 0) { 
      hasAlpha = TRUE; 
      break; 
     } 
    } 

    // If no alpha values available, apply the mask bitmap 
    if (!hasAlpha) { 
     // Extract the mask bitmap 
     int32_t* maskBits = new int32_t[nBits]; 
     GetDIBits(dc, iconInfo.hbmMask, 0, bm.bmHeight, maskBits, &bmi, DIB_RGB_COLORS); 
     // Copy the mask alphas into the color bits 
     for (int i = 0; i < nBits; i++) { 
      if (maskBits[i] == 0) { 
       colorBits[i] |= 0xff000000; 
      } 
     } 
     delete[] maskBits; 
    } 

    // Release DC and GDI bitmaps 
    ReleaseDC(NULL, dc); 
    ::DeleteObject(iconInfo.hbmColor); 
    ::DeleteObject(iconInfo.hbmMask); 

    // Create GDI+ Bitmap 
    Gdiplus::Bitmap* bmp = new Gdiplus::Bitmap(bm.bmWidth, bm.bmHeight, bm.bmWidth*4, PixelFormat32bppARGB, (BYTE*)colorBits); 
    BITMAP_AND_BYTES ret = {bmp, colorBits}; 

    return ret; 
} 

static void saveFileIconAsPng(int shilsize, const std::wstring& fname, const std::wstring& pngFile) { 
    HICON hIcon = getShellIcon(shilsize, fname); 
    BITMAP_AND_BYTES bbs = createAlphaChannelBitmapFromIcon(hIcon); 

    IStream* fstrm = NULL; 
    SHCreateStreamOnFile(pngFile.c_str(), STGM_WRITE|STGM_CREATE, &fstrm); 
    bbs.bmp->Save(fstrm, &g_pngClsid, NULL); 
    fstrm->Release(); 

    delete bbs.bmp; 
    delete[] bbs.bytes; 
    DestroyIcon(hIcon); 
} 

例invokation:

GdiplusStartup(...); 
GetEncoderClsid(L"image/png", &g_pngClsid); 

wstring fname = L"d:\\index.html"; 
wstring pngFile = L"d:\\icons\\index.html.png"; 
saveFileIconAsPng(SHIL_JUMBO, fname, pngFile); 

GdiplusShutdown(...); 
+0

不错,谢谢。 – noober 2014-10-06 11:40:37