title: windows cannot load bitmap image
body: <p>This program just shows a blank window.</p>
  
  <p>I want to load bitmap image randomly onto the screen. I am not able to figure this out. Even if I give different name to image in this program the program still executes without any error and shows the blank window.</p>
  
  <p>Please help me!   </p>
  
  <pre><code>#ifndef UNICODE
  #define UNICODE
  #endif
  
  /*have used unicode */
  
  #include&lt;Windows.h&gt;
  #include&lt;iostream&gt;
  #include&lt;time.h&gt;
  using namespace std;
  
  bool gameover =FALSE;
  const wchar_t classname[] = L"My first procedure and main";
  HWND window;
  HDC device;
  LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
  
  
  ATOM MyRegisterClass(HINSTANCE hInstance)
  {
      //set the new windows properties 
      WNDCLASS wc = {};
  
      wc.lpfnWndProc=(WNDPROC)WindowProc;
      wc.hInstance=hInstance;
      wc.style=CS_HREDRAW|CS_VREDRAW;
      wc.hbrBackground=(HBRUSH)GetStockObject(COLOR_WINDOW +1);
      wc.lpszClassName=classname;
  
      return RegisterClass(&amp;wc);
  }
  
  bool InitInstance(HINSTANCE hInstance,int nCmdShow)
  { 
      //Create a new window
      window=CreateWindowEx(0,
                            classname,
                            L"My Program",
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hInstance,
                            NULL );
  
      //Any error in creating the window ?
      if(window==0)
          return 0;
  
      //Display the window 
      ShowWindow(window,nCmdShow);
      UpdateWindow(window);
      device=GetDC(window);
      return 1;
  }
  
  bool Game_Init()
  {
      srand(time(NULL));
      return 1;
  }
  
  // This function handles the loading of bitmap image into the window
  void DrawBitmap( char *filename,int x,int y)
  {
      //Load the bitmap image
      HBITMAP image=(HBITMAP)LoadImage(0,L"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
      BITMAP bm;
      GetObject(image,sizeof(BITMAP),&amp;bm);
      HDC hdcdevice=CreateCompatibleDC(device);
      SelectObject(hdcdevice,image);
      BitBlt(device,x,y,bm.bmWidth,bm.bmHeight,hdcdevice,0,0,SRCCOPY);
  
      //delete the device context and bitmap
      DeleteDC(hdcdevice);
      DeleteObject((HBITMAP)image);
  }
  
  void Game_Run()
  {
      if(gameover==true)
          return;
      RECT rect;
      GetClientRect(window,&amp;rect);
  
      //draw bitmap at random location
      int x=rand() % (rect.right-rect.left);
      int y=rand() %(rect.bottom-rect.top);
  
      DrawBitmap("image.bmp",x,y);
  }
  
  void Game_End()
  {
      //Free the device
      ReleaseDC(window,device);
  }
  
  int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE,PWSTR PCmdLine,int nCmdShow)
  {   
      //Register the class    
      MyRegisterClass(hInstance);
  
      //Initialize application
      if(!InitInstance(hInstance,nCmdShow))
          return 0;
  
      //Declare variables
      MSG msg ={ };
  
      //Initialize the game
      if(!Game_Init())
          return 0;
  
      //main message loop
      while(!gameover)
      {
          if (PeekMessage(&amp;msg,NULL,0,0,PM_REMOVE))
          {
              TranslateMessage(&amp;msg);
              DispatchMessage(&amp;msg);
          }
          Game_Run();
      }
      Game_End();
  
      return msg.wParam;
  }
  
  //This is window procedure function for handling closing of windows.
  LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  {   
      switch(uMsg) 
      { 
          case WM_CLOSE: 
              if(MessageBox(hwnd,L"Do you want to close this window",NULL,MB_OKCANCEL)==IDOK)           
                  gameover=true;
              DestroyWindow(hwnd);
  
  
      }
      return DefWindowProc(hwnd,uMsg,wParam,lParam);
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/zY6vO.png" alt="This is the output of my program"></p>