为什么CreateDIBSection返回NULL?

zzlelutf  于 2023-08-03  发布在  其他
关注(0)|答案(1)|浏览(95)

我试图得到一个应用程序的客户端区域的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。
任何帮助将不胜感激。

rvpgvaaj

rvpgvaaj1#

基于@RbMm的评论和Alpha Blending a Bitmap example的总结是BITMAPINFO结构没有正确初始化。初始化是初始化临时tmpbmphdr。像Alpha Blending a Bitmap example一样,将内存清零也是必要的。

// zero the memory for the bitmap info 
    ZeroMemory(&bmi, sizeof(BITMAPINFO));

    // setup bitmap info  
    // set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas. 
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = ulBitmapWidth = ulWindowWidth - (ulWindowWidth/5)*2;
    bmi.bmiHeader.biHeight = ulBitmapHeight = ulWindowHeight - (ulWindowHeight/5)*2;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;         // four 8-bit components 
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = ulBitmapWidth * ulBitmapHeight * 4;

    // create our DIB section and select the bitmap into the dc 
    hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);

字符串

相关问题