2010-10-25 73 views
0

我周围中搜索使用谷歌为位图,但我对如何从资源加载的图像(PNG在我的情况)完全糊涂了,然后在内存中转换为位图用于我的启动画面。我已经阅读了关于GDI +和libpng的内容,但我并不知道如何去做我想做的事情。谁能帮忙?加载从资源的图像和转换内存

+0

如果您可以使用JPEG代替,那么OleLoadPicture和朋友应该做的伎俩。 – 2010-10-25 06:54:11

+0

但我不认为有没有办法来存储JPEG文件没有它压缩...而我需要保持阿尔法通道 – 2010-10-25 06:56:13

回答

0

我最终使用PicoPNG到PNG转换为我然后手动从一个池莉构建位图的二维矢量。我的最终代码看起来像这样:

HBITMAP LoadPNGasBMP(const HMODULE hModule, const LPCTSTR lpPNGName) 
{ 
    /* First we need to get an pointer to the PNG */ 
    HRSRC found = FindResource(hModule, lpPNGName, "PNG"); 
    unsigned int size = SizeofResource(hModule, found); 
    HGLOBAL loaded = LoadResource(hModule, found); 
    void* resource_data = LockResource(loaded); 

    /* Now we decode the PNG */ 
    vector<unsigned char> raw; 
    unsigned long width, height; 
    int err = decodePNG(raw, width, height, (const unsigned char*)resource_data, size); 
    if (err != 0) 
    { 
     log_debug("Error while decoding png splash: %d", err); 
     return NULL; 
    } 

    /* Create the bitmap */ 
    BITMAPV5HEADER bmpheader = {0}; 
    bmpheader.bV5Size = sizeof(BITMAPV5HEADER); 
    bmpheader.bV5Width = width; 
    bmpheader.bV5Height = height; 
    bmpheader.bV5Planes = 1; 
    bmpheader.bV5BitCount = 32; 
    bmpheader.bV5Compression = BI_BITFIELDS; 
    bmpheader.bV5SizeImage = width*height*4; 
    bmpheader.bV5RedMask = 0x00FF0000; 
    bmpheader.bV5GreenMask = 0x0000FF00; 
    bmpheader.bV5BlueMask = 0x000000FF; 
    bmpheader.bV5AlphaMask = 0xFF000000; 
    bmpheader.bV5CSType = LCS_WINDOWS_COLOR_SPACE; 
    bmpheader.bV5Intent = LCS_GM_BUSINESS; 
    void* converted = NULL; 
    HDC screen = GetDC(NULL); 
    HBITMAP result = CreateDIBSection(screen, reinterpret_cast<BITMAPINFO*>(&bmpheader), DIB_RGB_COLORS, &converted, NULL, 0); 
    ReleaseDC(NULL, screen); 

    /* Copy the decoded image into the bitmap in the correct order */ 
    for (unsigned int y1 = height - 1, y2 = 0; y2 < height; y1--, y2++) 
     for (unsigned int x = 0; x < width; x++) 
     { 
      *((char*)converted+0+4*x+4*width*y2) = raw[2+4*x+4*width*y1]; // Blue 
      *((char*)converted+1+4*x+4*width*y2) = raw[1+4*x+4*width*y1]; // Green 
      *((char*)converted+2+4*x+4*width*y2) = raw[0+4*x+4*width*y1]; // Red 
      *((char*)converted+3+4*x+4*width*y2) = raw[3+4*x+4*width*y1]; // Alpha 
     } 

    /* Done! */ 
    return result; 
} 
1

GDI +支持PNG直接。见herehere

编辑:GDI+ documentation提供了一些关于如何在DLL中使用GDI +的建议。就你而言,最好的解决方案可能是定义客户端代码需要调用的初始化和拆卸函数。

+0

好吧。看起来我无法使用GDI +来处理我正在做的事情,因为这些都是在DllMain中完成的,而且它们所调用的函数都完成了。你有什么其他的建议? – 2010-10-26 09:43:08

+0

如果我可以编辑调用dll的程序,我会这样做(我读过它),但是我不能。所以我真的不认为我可以使用GDI +。我目前正在研究使用WIC将我的PNG转换为HBITMAP – 2010-10-26 11:06:18