【问题标题】:why CreateDIBSection() fails with certian BITMAPINFO?为什么 CreateDIBSection() 因某些 BITMAPINFO 而失败?
【发布时间】:2011-01-18 09:54:28
【问题描述】:

我正在尝试使用CreateDIBSection

问题:

在 Windows XP 中,我尝试调用 CreateDIBSection,它返回 NULLGetLastError = 0

当我尝试将屏幕分辨率更改为 2048 x 1536 时,它会返回正确的值。

我测试过这个函数和nMemSize有一定的关系(不一定是小数)。

问题:

是否有任何保证方法可以确保CreateDIBSection 返回正确的值?

nScreenWidth = 1024;
nScreenHeight= 768;
 = nScreenWidth*nScreenHeight*3*7
HDC hdc = ::GetDC(hWnd);
m_hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL);

BITMAPINFO bmpInfo = {0};
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = nScreenWidth;
bmpInfo.bmiHeader.biHeight = nScreenHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = 0;
bmpInfo.bmiHeader.biSizeImage = nMemSize;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiColors[0].rgbBlue = 204;
bmpInfo.bmiColors[0].rgbGreen = 204;
bmpInfo.bmiColors[0].rgbRed = 204;
bmpInfo.bmiColors[0].rgbReserved = 0;
PVOID pvBits;

m_hBmpAllWstDskWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, m_hBmpMapFile, 0); 

【问题讨论】:

  • 嗯,目前还不清楚它为什么会失败。我认为你的问题应该是,“为什么 CreateDIBSection 失败了?”。如果你能得到答案,那么你应该能够解决你真正的问题。
  • 好的,我已经改了。我认为当 nMemSize 更改为特定数字时,真正的问题是 CreateDIBSection 失败。有时它会失败,说 10000,但是当你将 nMemsize 更改为更大的 20000 时,它会成功。
  • @yyy:发布bmpinfo.bmiHeader的所有字段。你赋予他们什么价值?
  • 请提供完整的代码。我们更容易说出(或猜测)问题可能出在哪里。为什么* 7nMemSize 等式中?
  • 对不起,我刚刚编辑了。 *3 表示 RGB 字节,*7 表示屏幕大小的 7 倍。我正在制作一个大的水平全景屏幕。

标签: c++ windows-xp bitmap createdibsection


【解决方案1】:

我怀疑问题可能包含在您未包含的代码部分中(在省略号中......)。所以我推荐:

  • 检查您的设备上下文是否有效
  • 零内存
  • 添加结构大小
  • 和位图尺寸
  • 移动 GetLastError 调用以确保设备上下文有效(可能早期的 API 调用失败)

我添加了上面的建议后,下面的代码似乎可以工作,希望对您有所帮助:

        HDC hdc = ::GetDC(hWnd); 
        int nScreenWidth = 1024; 
        int nScreenHeight= 768; 
        int nMemSize = nScreenWidth*nScreenHeight*3*7;
        HANDLE m_hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL); 
        BITMAPINFO bmpInfo; 
        //clear the memory
        ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
        //struct size
        bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
        //dimensions
        bmpInfo.bmiHeader.biWidth = nScreenWidth;
        bmpInfo.bmiHeader.biHeight = nScreenHeight;
        bmpInfo.bmiHeader.biPlanes = 1; 
        bmpInfo.bmiHeader.biBitCount = 32; 
        bmpInfo.bmiHeader.biSizeImage=nMemSize; 
        void *pvBits = NULL;
        HANDLE m_hBmpAllWstDskWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, m_hBmpMapFile, 0);
        int nError = ::GetLastError();      

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 2012-09-08
    • 2013-09-24
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2013-04-08
    相关资源
    最近更新 更多