2013-10-09 45 views
0

如何創建ARGB格式的DIB。我想用這個DIB將一個圖像(它有一些透明的部分)blit。 我用下面的代碼嘗試過,但它不能正常工作創建ARGB DIB

unsigned char * rawdata; ==> Filled by Qimage Raw Data 
    unsigned char * buffer = NULL; 
    memset(&bmi, 0, sizeof(bmi)); 
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    bmi.bmiHeader.biWidth = width;/* Width of your image buffer */ 
    bmi.bmiHeader.biHeight = -height; /* Height of your image buffer */ 
    bmi.bmiHeader.biPlanes = 1; 
    bmi.bmiHeader.biBitCount = 32; 
    bmi.bmiHeader.biCompression = BI_RGB; 
    HBITMAP g_dibbmp = CreateDIBSection(hDesktopDC, &bmi, DIB_RGB_COLORS, (void **)&buffer, 0, 0); 
    if (!buffer) 
    { /* ERROR */ 
     printf("ERROR DIB could not create buffer\n"); 
    } 
    else 
    { 
     printf("DIB created buffer successfully\n"); 
     memcpy(buffer,rawdata,sizeof(rawdata)); 
    } 

請幫忙。

Reagards,

Techtotie。

回答

2

這是我從工作代碼片段放在一起的片段。我看到的主要區別是設置掩碼位和使用memsection。

// assumes height and width passed in 
int bpp = 32; // Bits per pixel 
int stride = (width * (bpp/8)); 
unsigned int byteCount = (unsigned int)(stride * height); 

HANDLE hMemSection = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, byteCount, NULL); 
if (hMemSection == NULL) 
    return false; 

BITMAPV5HEADER bmh; 
memset(&bmh, 0, sizeof(BITMAPV5HEADER)); 
bmh.bV5Size = sizeof(BITMAPV5HEADER); 
bmh.bV5Width = width; 
bmh.bV5Height = -height; 
bmh.bV5Planes = 1; 
bmh.bV5BitCount = 32; 
bmh.bV5Compression = BI_RGB; 
bmh.bV5AlphaMask = 0xFF000000; 
bmh.bV5RedMask = 0x00FF0000; 
bmh.bV5GreenMask = 0x0000FF00; 
bmh.bV5BlueMask = 0x000000FF; 

HDC hdc = ::GetDC(NULL); 
HBITMAP hDIB = ::CreateDIBSection(hdc, (BITMAPINFO *) &bmh, DIB_RGB_COLORS, 
    &pBits, hMemSection, (DWORD) 0); 
::ReleaseDC(NULL, hdc); 

// Much later when done manipulating the bitmap 
::CloseHandle(hMemSection);