0

如何创建 ARGB 格式的 DIB。我想使用这个 DIB 对一个图像(其中有一些透明的部分)进行 blit。我尝试使用以下代码,但无法正常工作

   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));
    }

请帮忙。

问候,

技术。

4

2 回答 2

3

这是我从工作代码中拼凑出来的一个片段。我看到的主要区别是设置掩码位和使用 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 );
于 2013-10-19T00:41:05.657 回答
1

Thanks for your answer.

But my problem got solved. It was not actually the problem with the DIB creation. It was due to the wrong API that I was using for Blitting.

I was using BitBlt for blitting but this API does not take care of the Alpha gradient. Instead of it I tried TransparentBlt (Refer : http://msdn.microsoft.com/en-us/library/windows/desktop/dd145141(v=vs.85).aspx)

and it worked as this API takes care of copying the Alpha values from Source DC to destination DC.

于 2013-10-20T13:45:36.940 回答