CreateDIBSection创建位图

24位位图:

LPBYTE DIBData;

HBITMAP HBitMapDst;

BITMAPINFO *pbinfo = NULL;
pbinfo = (BITMAPINFO *)calloc(1, sizeof(BITMAPINFO)) ;
if(pbinfo == NULL)
{
return;
}
pbinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
pbinfo->bmiHeader.biWidth = Width;//指定图像尺寸
pbinfo->bmiHeader.biHeight =Length ;
pbinfo->bmiHeader.biPlanes = 1;
pbinfo->bmiHeader.biBitCount = 24;
pbinfo->bmiHeader.biCompression = 0;
pbinfo->bmiHeader.biSizeImage = 0 ;
pbinfo->bmiHeader.biXPelsPerMeter = 2834;
pbinfo->bmiHeader.biYPelsPerMeter = 2834;
pbinfo->bmiHeader.biClrUsed = 0;
pbinfo->bmiHeader.biClrImportant = 0;


HBitMapDst = CreateDIBSection(NULL, pbinfo, DIB_RGB_COLORS, (void **)&DIBData, NULL, 0) ;
free(pbinfo) ;


if((HBitMapDst == NULL) || (DIBData == NULL))
{
return;
}
BITMAP DstBitmap;

GetObject(HBitMapDst,sizeof(BITMAP),&DstBitmap);

32位位图:

LPBYTE DIBData;
BITMAPINFO *pbinfo = NULL;
pbinfo = (BITMAPINFO *)calloc(1, sizeof(BITMAPINFO) + 4 * sizeof(INT)) ;
if(pbinfo == NULL)
{
return;
}
pbinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
pbinfo->bmiHeader.biWidth = Width;//指定图像尺寸
pbinfo->bmiHeader.biHeight = Height;
pbinfo->bmiHeader.biPlanes = 1;
pbinfo->bmiHeader.biBitCount = 32;
pbinfo->bmiHeader.biCompression = BI_ALPHABITFIELDS;
pbinfo->bmiHeader.biSizeImage = 0 ;
pbinfo->bmiHeader.biXPelsPerMeter = 2834;
pbinfo->bmiHeader.biYPelsPerMeter = 2834;
pbinfo->bmiHeader.biClrUsed = 0;
pbinfo->bmiHeader.biClrImportant = 0;


int *pMask = (int*)&(pbinfo->bmiColors[0]) ;
*pMask++ = 0x00FF0000 ;
*pMask++ = 0x0000FF00 ;
*pMask++ = 0x000000FF ;
*pMask++ = 0xFF000000 ;


hDstBitmap = CreateDIBSection(NULL, pbinfo, DIB_RGB_COLORS, (void **)&DIBData, NULL, 0) ;
free(pbinfo) ;


if((hDstBitmap == NULL) || (DIBData == NULL))
{
return;
}

你可能感兴趣的:(c/c++)