2010-10-25 40 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