windows - 创建 ARGB DIB

  显示原文与译文双语对照的内容
84 1

如何用ARGB格式创建 DIB 。 我想用这个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 buffern");
 }
 else
 {
 printf("DIB created buffer successfullyn");
 memcpy(buffer,rawdata,sizeof(rawdata));
 }

请帮助。

Reagards

Techtotie 。

时间: 原作者:

51 1

这是我从工作代码中拼凑起来的Fragment 。 我看到的主要区别是设置掩码位并使用 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 );

原作者:
...