为什么 CreateDIBSection() 对某些 BITMAPINFO 失败?

发布于 2024-10-12 13:05:17 字数 1301 浏览 4 评论 0原文

我正在尝试使用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); 

I am trying to use CreateDIBSection.

Problem:

In Windows XP, I tried to call CreateDIBSection, it returns NULL and GetLastError = 0

When I try to change the screen resolution, for example to 2048 x 1536, it returns correct value.

I have tested this function has some relationship with nMemSize (not necessarily small number).

Question:

Is there any guarantee way to ensure CreateDIBSection returns correct value?

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); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2024-10-19 13:05:17

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

  • 检查您的设备上下文是否有效
  • ZeroMemory
  • 添加结构大小
  • 和位图尺寸
  • 移动 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();      

I suspect the problem might be contained in the section of code that you did not include (in the elipses ...). So I recommend:

  • Check your Device Context is valid
  • ZeroMemory
  • Add the structure size
  • And bitmap dimensions
  • Move the GetLastError calls to ensure that the Device Context is valid (perhaps an earlier API call fails)

The code below seems to work after I added the recommendations above, I hope it helps:

        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();      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文