Thread: Cant get an image to show on my HDC

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Cant get an image to show on my HDC

    I cant seem to get a BITMAP to show on my HDC
    //globals
    Code:
    //Win32 Bitmap stuff
    HANDLE hImage;
    HWND button_hWnd;
    HDC hdc;
    HDC c_hdc;
    BITMAP Bitmap;
    SIZE ImageSize;
    Code:
    hImage = LoadImage(hInstance,"Test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
       if(hImage != INVALID_HANDLE_VALUE)
       {
    	   MessageBox(NULL,"Loaded Test.bmp","",0);
       }
       hdc = GetDC(hWnd);
       c_hdc = CreateCompatibleDC(hdc);
       GetObject(c_hdc,sizeof(BITMAP),&Bitmap);
       ImageSize.cx = Bitmap.bmWidth;
       ImageSize.cy = Bitmap.bmHeight;
    in the Wndproc WM_PAINT message

    Code:
    case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		SelectObject(c_hdc,hImage);
    		BitBlt(hdc,0,0,ImageSize.cx,ImageSize.cy,c_hdc,0,0,SRCCOPY);
    		// TODO: Add any drawing code here...
    		EndPaint(hWnd, &ps);
    		break;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > hImage = LoadImage(hInstance,"Test.bmp",IMAGE_BITMAP,0,0,LR _LOADFROMFILE);
    But is this done in a different WM_MESSAGE case?

    If so, you need to make things like c_hdc to be static variables, so they're preserved from one call to your winproc to the next.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    no its done straight after the main window is created

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Fixing your error checking, INVALID_HANDLE_VALUE is a backwards compatible value used only with CreateFile. If LoadImage fails, it retuns NULL.

    Also, when you perform SelectObject, you're pushing a 1x1 monochrome bitmap (included free with the memory DC) into the ether. This is bad. Store the return value and put it back before EndPaint.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 07-01-2011, 12:29 PM
  2. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  3. I can't make bmp image files show up in an SDL window
    By Lucas89 in forum Game Programming
    Replies: 5
    Last Post: 05-25-2009, 01:04 PM
  4. Problems with Image Magick [Unable to Quantisize Image]
    By Maragato in forum C Programming
    Replies: 1
    Last Post: 09-18-2006, 10:41 PM
  5. show() method does not show...
    By kocika73 in forum C++ Programming
    Replies: 12
    Last Post: 04-09-2006, 09:27 PM