Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i think i just found the solution. 1) the problem is that backbuffer surface and source surface are of different formats - that is why exception was thrown. 2) the image path needed double slash "C\\......." ;)

================================= I have been googling for a while but still cant come up with a solution to the problem - D3DXGetImageInfoFromFile function just doesn't load any images.

it compiles perfectly well, but throws an exception when it comes to the D3DXGetImageInfoFromFile function.

Here is the complete listing:

//================================================================================================================================// including stuff //=================//

#include <windows.h>
#include <string>
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9tex.h>

//================================================================================================================================// handling windows //================//

HWND wndHandle;
bool stopEverything(false);

//================================================================================================================================// handling directx needs //==========//

LPDIRECT3D9                 pD3D;                       // the Direct3D object
LPDIRECT3DDEVICE9           pd3dDevice;                 // the Direct3D device
IDirect3DSurface9           *Surface = NULL;            // image surface
IDirect3DSurface9           *BackBuffer = NULL;         // back buffer surface

//================================================================================================================================// declaring functions //=============//

bool initWindow(HINSTANCE hInstance);
bool initDirect3D(void);
void cleanUp (void);
void render(void);

//================================================================================================================================// code starts here //================//

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    if (!initWindow(hInstance)) return false;

    if (!initDirect3D())
        return false;

    MSG msg;
    ZeroMemory(&msg, sizeof(msg));

        if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
             TranslateMessage (&msg);
             DispatchMessage (&msg);
         } else {

    return static_cast<int>(msg.wParam);

bool initWindow(HINSTANCE hInstance )
    WNDCLASSEX wcex;

    wcex.cbSize             = sizeof(WNDCLASSEX);              = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc        = WndProc;
    wcex.cbClsExtra         = 0;
    wcex.cbWndExtra         = 0;
    wcex.hInstance          = hInstance;
    wcex.hIcon              = LoadIcon(0, IDI_APPLICATION);
    wcex.hCursor            = LoadCursor(0, IDC_ARROW);
    wcex.hbrBackground      = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
    wcex.lpszMenuName       = 0L;
    wcex.lpszClassName      = L"DirectXTemplate";
    wcex.hIconSm            = 0;


    wndHandle = CreateWindow(L"DirectXTemplate", L"DirectX Template", WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);

    if (!wndHandle) return false;

    ShowWindow(wndHandle, SW_SHOW);

    return true;

    switch (message)
        case WM_DESTROY:

            return DefWindowProc(hWnd, message, wParam, lParam);

bool initDirect3D(void)
    pD3D = NULL;
    pd3dDevice = NULL;

    // create the DirectX object
    if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return false;

    std::wstring wsPath = L"C:\wood.bmp"; // path to the image

    if (FAILED(D3DXGetImageInfoFromFile(wsPath.c_str(), &Info)))
        MessageBox(NULL, L"You're a terrible programmer!", L"ERROR", MB_OK);
        stopEverything = true;
    } else {
        pd3dDevice -> CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);
        D3DXLoadSurfaceFromFile(Surface, NULL, NULL, wsPath.c_str(), NULL, D3DX_FILTER_NONE, 0, NULL);
        pd3dDevice -> GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &BackBuffer);

    // fill the presentation parameters structure
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed          = TRUE;
    d3dpp.SwapEffect        = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat  = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount   = 1;
    d3dpp.BackBufferHeight  = 480;
    d3dpp.BackBufferWidth   = 640;
    d3dpp.hDeviceWindow     = wndHandle;

    // create a default DirectX device
    if (FAILED(pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))) return false;

    return true;

void render(void)
    // check to make sure you have a valid Direct3D device
    if (NULL == pd3dDevice) return;

    pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);   // clear the back buffer to a blue color

    if (SUCCEEDED(pd3dDevice -> BeginScene()))
        if (stopEverything == false)
            pd3dDevice -> UpdateSurface(Surface, NULL, BackBuffer, NULL);

        // end the scene
        pd3dDevice -> EndScene();

    // present the back buffer contents to the display
    pd3dDevice -> Present(NULL, NULL, NULL, NULL);

void cleanUp (void)
    // release the device and the Direct3D object
    if (pd3dDevice != NULL) pd3dDevice -> Release();
    if (pD3D != NULL) pD3D -> Release();
share|improve this question
I'm assuming C:\wood.bmp exists on your computer? – thedaian Dec 23 '11 at 22:26
yes, of course) – Abdulali Dec 23 '11 at 23:01
Have you tried running this with the debug device and checking the output for D3D problems? What is the exception and debug output? What is the HRESULT of the GetInfoFromFile call? Have you tried a microsoft sample that has a working function call and compared? I'm just full of questions when debugging. – Patrick Hughes Dec 24 '11 at 0:07
debugging revealed strange hex exception code. even google didnt find it. anyways i got this code program running after all. just edited my question – Abdulali Dec 24 '11 at 0:26
the solution is on the top – Abdulali Dec 24 '11 at 0:27

Your D3D device needs to exist before you can load the image and create the surface.

share|improve this answer
i tried putting everything concerning loading image into the render() function - which means d3d device existed by that time. still had errors. – Abdulali Dec 23 '11 at 23:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.