Question

How to create a DIB with ARGB format. I want to blit a image(that has some part transparent in it ) using this DIB. I tried with the following code but its not working properly

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

Please help.

Reagards,

Techtotie.

Was it helpful?

Solution 2

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.

OTHER TIPS

Here's a snippet I put together from pieces of working code. The main difference I see is setting the mask bits and using 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 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top