我试图得到一个应用程序的客户端区域的DI位图。客户区是一个对话框窗口,我使用了CreateDIBSection函数,该函数返回NULL,GetLastError返回0。
代码如下:
TRESULT GetDIBPage(_IN HWND inobj, \
_INOUT void **outobj){
//Variable declarations
void *tmpbits = 0;
char *tmpstr = 0;
BITMAPINFO tmpbmpinfo;
BITMAPINFOHEADER tmpbmphdr;
POINT tmppoint;
HDC tmphdc;
HWND tmphndwindow = 0;
int tmpn = 0;
int ret = 0;
int ret2 = 0;
//Argument checks
if(inobj == NULL){
return TPTRISNULL;
} //EndIf
if(outobj == NULL){
return TPTRISNULL;
} //EndIf
//Initializations
tmphndwindow = inobj;
//Main logic
//Start to get the DIB.
//Get a device context for the window.
tmphdc = GetDC(tmphndwindow);
//Get the width in bytes.
tmpn = (3 * 408);
//Calculate the stride.
if((tmpn % 4) > 0){
//Round to the nearest 32 bits.
tmpn = (((tmpn / 4) + 1) * 4);
} //EndIf
//Calculate the DI bitmap size.
//Set the tmpbmphdr variable.
tmpbmphdr = tmpbmpinfo.bmiHeader;
//Set the members of the bitmap info
//header
tmpbmphdr.biSize = sizeof(BITMAPINFOHEADER);
tmpbmphdr.biWidth = tmpn;
tmpbmphdr.biHeight = 600;
tmpbmphdr.biPlanes = 1;
tmpbmphdr.biBitCount = 24;
tmpbmphdr.biCompression = BI_RGB;
tmpbmphdr.biSizeImage = (tmpn * 600);
tmpbmphdr.biXPelsPerMeter = 0;
tmpbmphdr.biYPelsPerMeter = 0;
tmpbmphdr.biClrUsed = 0;
tmpbmphdr.biClrImportant = 0;
//Get the DI Bitmap.
CreateDIBSection(tmphdc, \
&tmpbmpinfo, \
DIB_RGB_COLORS, \
&tmpbits, \
NULL, \
0);
//Check if tmpbits is NULL.
if(tmpbits == NULL){
tmpstr = (char*)calloc(513, sizeof(char));
sprintf(tmpstr, \
"Creating the DIB failed.\n %d", \
GetLastError());
MessageBox(NULL, \
tmpstr, \
"Error", \
MB_OK);
free(tmpstr);
return TPTRISNULL;
}
//Free variables
ReleaseDC(tmphndwindow, \
tmphdc);
//Returns
*outobj = tmpbits;
return TSUCCESS;
} //End GetDIBPage
字符串
当我运行程序时,出现错误消息,指示tmpbits被设置为NULL。
任何帮助将不胜感激。
1条答案
按热度按时间rvpgvaaj1#
基于@RbMm的评论和Alpha Blending a Bitmap example的总结是BITMAPINFO结构没有正确初始化。初始化是初始化临时
tmpbmphdr
。像Alpha Blending a Bitmap example一样,将内存清零也是必要的。字符串