我知道什么时候有什么东西可以在调试中运行而不是在发布时,有些东西没有正确初始化,使用或清理,但我找不到。
我首先创建一个像这样的dib部分:
BITMAPINFO bmi;
HBITMAP bitmap;
LPBYTE pBits;
// Initialize header to 0s.
ZeroMemory(&bmi, sizeof(bmi));
// Fill out the fields you care about.
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = w;
bmi.bmiHeader.biHeight = h;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
HDC dc = GetDC(hw);
bitmap = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0);
for (int y = 0; y < w*h; y++)
{
pBits[y * 3 + 0] = 200;
pBits[y * 3 + 1] = 200;
pBits[y * 3 + 2] = 200;
}
然后我这样渲染:
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP oldBmp= (HBITMAP)SelectObject(hdcMem, bitmap);
BitBlt(hdc, xabs, yabs, width, height,
hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBmp);
DeleteDC(hdcMem);
在调试模式下,一切都很好,但在发布时,图像是白色的。
此外,如果我使用bitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(idb), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
,图像也会正确呈现。
答案 0 :(得分:0)
我已经通过从WM_TIMER和后来的WM_PAINT处理程序调用它来测试你的代码,我无法重现错误的行为win Release版本。测试了VS 2013,2008甚至6.0,但仅限于Windows 10。
我发现问题(但它们不会导致构建模式之间的差异)。
HDC dc = GetDC(hw);
,但没有ReleaseDC
。w
或width
的价值。如果24位模式下它不是4的倍数,你将会遇到一些视觉故障。在我的测试代码中,我必须猜测几个值(xabs
,yabs
)。检查您是否正确初始化它们。
void TEST( HWND hwnd )
{
RECT rect;
GetClientRect( hwnd, &rect );
int width = rect.right;
int height = rect.bottom;
if ( width < 1 || height < 1 )
return;
// width correction, up to muliple of 4.
width = ( width + 3 ) & ~3;
int xabs = 0;
int yabs = 0;
BITMAPINFO bmi;
HBITMAP bitmap;
LPBYTE pBits;
// Initialize header to 0s.
ZeroMemory(&bmi, sizeof(bmi));
// Fill out the fields you care about.
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
HDC hdc = GetDC(hwnd);
if ( hdc == 0 )
return;
bitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0);
if ( bitmap == 0 )
{
ReleaseDC(hwnd,hdc)
return;
}
for (int y = 0; y < width*height; y++)
{
// Use more interesting visual.
pBits[y * 3 + 0] = y/width; //200;
pBits[y * 3 + 1] = 200;
pBits[y * 3 + 2] = 200;
}
HDC hdcMem = CreateCompatibleDC(hdc);
if ( hdcMem )
{
HBITMAP oldBmp= (HBITMAP)SelectObject(hdcMem, bitmap);
BitBlt(hdc, xabs, yabs, width, height,
hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBmp);
DeleteObject( bitmap );
DeleteDC(hdcMem);
}
// Release device - missing in oryginal code.
ReleaseDC(hwnd,hdc);
}