CreateDIBSection() returning NULL
Don't know what happened... But it WORKS NOW Thanks.
The CreateDIBSection() function that I'm learning is not working. I have tried to closely follow the codes in many tutorials
and Peztold but its not working. May be I'm missing something.
Here is the error and a reduced version showing the same problem.
Code:
Error got from 'GetLastError()' :The specified image type cannot be found in the image file.
But I'm not using any image file ! Am I missing something ?
I tried many webpages on CreateDIBSection() and tried to closely follow them.
Code:
#include<windows.h>
const unsigned long WIDTH = 384, HEIGHT = 256; //dimensions of the bitmap.
inline void GetLastErrorMessage(){ //Formats GetLastError() value.
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL
);
// Display the string.
MessageBox( NULL, (const char*)lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
LRESULT CALLBACK MainProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){
static HBITMAP hBitmap;
static BITMAPINFOHEADER bmiHeader;
static HDC hMem;
static void *bits; //holds bitmap pixels.
static bool EveryThingIsRight = true;
switch(Msg){
case WM_CREATE:
{
GetLastErrorMessage(); //To make sure NO error happned before.
//a memory device context compatible with the screen to select the bitmap for BitBlting.
hMem = CreateCompatibleDC(NULL);
if( !hMem ){
MessageBox(hWnd, "Memory Device Context could not be Created !",
"ERROR", MB_ICONERROR | MB_OK);
EveryThingIsRight = false;
GetLastErrorMessage();
}
bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth = WIDTH;
bmiHeader.biHeight = HEIGHT;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 24;
bmiHeader.biCompression = BI_RGB;
bmiHeader.biSize = 0;
bmiHeader.biXPelsPerMeter = 0;
bmiHeader.biYPelsPerMeter = 0;
bmiHeader.biClrUsed = 0;
bmiHeader.biClrImportant = 0;
hBitmap = CreateDIBSection(hMem, (BITMAPINFO*)&bmiHeader,
DIB_RGB_COLORS,
&bits, NULL, 0);
if( !hBitmap ){
MessageBox(hWnd, "DIB section could not be created !",
"ERROR", MB_ICONERROR | MB_OK);
EveryThingIsRight = false;
GetLastErrorMessage();
}
if( !SelectObject(hMem, hBitmap) ){
MessageBox(hWnd, "DIB section could not be Selected !",
"ERROR", MB_ICONERROR | MB_OK);
EveryThingIsRight = false;
GetLastErrorMessage();
}
}
break;
case WM_PAINT:
{
//if( ! EveryThingIsRight ) //If HDc && hBitmap is allright.
//break;
PAINTSTRUCT ps;
RECT sc;
GetClientRect(hWnd, &sc);
HDC hdc = BeginPaint(hWnd, &ps);
BitBlt(hdc, 0, 0, sc.right, sc.bottom, hMem, 0, 0, SRCPAINT);
EndPaint(hWnd, &ps);
return 0;
}
break;
case WM_DESTROY:
DeleteDC(hMem);
DeleteObject(hBitmap);
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPI, LPSTR CmdL, int nCmdS)
{
WNDCLASSEX WndCls;
WndCls.cbSize = sizeof(WNDCLASSEX);
WndCls.style = 0;
WndCls.lpfnWndProc = MainProc;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = hInstance;
WndCls.hIcon = LoadIcon(NULL, IDI_WINLOGO);
WndCls.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCls.hbrBackground = HBRUSH( COLOR_3DFACE + 1);
WndCls.lpszMenuName = NULL;
WndCls.lpszClassName = "MainClass";
if( !RegisterClassEx(&WndCls) ){
return 1;
}
HWND hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "MainClass", "Preview Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 500,
NULL, NULL, hInstance, NULL);
if( !hWnd ){
return 1;
}
MSG Msg;
while( GetMessage(&Msg, NULL, 0, 0) > 0 ){
if( IsDialogMessage(hWnd, &Msg) )
continue;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
MSDN says that CreateDIBSection() returns only one type of error:
May be I should concentrate on that and proceed.
Code:
Windows NT/2000/XP: To get extended error information, call GetLastError. This can be the following value.
Value Meaning
ERROR_INVALID_PARAMETER One or more input parameters is invalid.