Thread: Display a bitmap image

  1. #1
    Registered User MysticMizra's Avatar
    Join Date
    Sep 2002
    Posts
    2

    Display a bitmap image

    I'm New to programming and can create a window but i can't seem to draw up a bitmap. I'm using dev c++ and just need some help please.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Ok..if you have the window, then you have 90% of the code done....

    If you just want the bitmap to appear on the window, then load a bitmap into your compiler's resource editor, save it as IDB_BITMAP1 and change your windows procedure like so

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, 
    						 UINT msg, 
    						 WPARAM wParam, 
    						 LPARAM lParam)
    {
    	HDC hdc,//This is for the device context of your window
    		memdc;//This DC will hold reference bitmap
    	static HBITMAP hBit;//The actual bitmap
    	PAINTSTRUCT ps;//For WS_PAINT
    	HGDIOBJ hTemp;//Temp. Hold return of SelectObject
    	static SIZE size;//Size of Bitmap
    
    
     	switch(msg) {
    	case WM_CREATE:	
    		size.cx = 50;//We want the bitmap to be 50x50
    		size.cy = 50;
    		hBit = (HBITMAP)LoadImage(GetModuleHandle(NULL),
    			MAKEINTRESOURCE(IDB_BITMAP1),IMAGE_BITMAP,
    			size.cx,size.cy,0);//Load our bitmap
    		if(!hBit)SendMessage(hWnd,WM_CLOSE,0,0);//Close if fail	
    		break;
    		case WM_PAINT:
    		hdc = BeginPaint(hWnd,&ps);//Do this at start of paint block
    		memdc = CreateCompatibleDC(hdc);//copy the dc
    		hTemp = SelectObject(memdc,hBit);//Select bitmap into dc
    		BitBlt(hdc,0,0,size.cx,size.cy,memdc,0,0,SRCCOPY);//paint to screen
    		SelectObject(memdc,hTemp);//restore the spare dc
    		DeleteDC(memdc);//destroy the spare
    		EndPaint(hWnd,&ps);//End Painting
    		break;
    
      case WM_CLOSE:
    		PostQuitMessage(0);
    		break;
      default:
    		return DefWindowProc(hWnd, msg, wParam, lParam);
     }
    
     return 0;
    }
    If you want a fool example go to Ken's site here

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>If you want a fool example go to Ken's site <<

    Not sure what to make of this, but I love the use of the word 'fool'; I may incorporate it into future descriptions.

    BTW, I like the use of HGDIOBJ in your example; looks like it sidesteps all that (c++) casting rather nicely.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Ken Fitlike
    >>If you want a fool example go to Ken's site <<

    Not sure what to make of this, but I love the use of the word 'fool'; I may incorporate it into future descriptions.

    BTW, I like the use of HGDIOBJ in your example; looks like it sidesteps all that (c++) casting rather nicely.
    ROFLMFAO!!!!!!

    Sorry Ken....I meant FULL but my pre-caffine mind had its own ideas......no Freudian slip I assure you ...I recommend your site most highly

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    And if you don't want to use resources...

    Code:
    
    
    /*my first Windows hack, before learnig about GetObject()...:D*/
    
    bool GetBitmapSize(char *filename, int *width, int *height) {
    
    FILE *image_file = fopen(filename, "rb");
    
    if(image_file == NULL)
     {
      *width = 0, *height = 0;
       return false;
     }
    
    int read = 0;
    
    fseek(image_file, 18, SEEK_SET);
    
    read = fread( &(*width), sizeof(int), 1, image_file);
    
    if(!read)
     {
      fclose(image_file);
      *width = 0, *height = 0;
      return false;
     }
    
    fseek(image_file, 22, SEEK_SET);
    
    read = fread( &(*height), sizeof(int), 1, image_file );
    
    if(!read)
     {
      fclose(image_file);
      *width = 0, *height = 0;
      return false;
     }
    
    fclose(image_file);
    
    return true;
    }
    
    
    
    
    
     
    
    HBITMAP LoadGraphic( char *file_name, int *width, int *height)
    {
      GetBitmapSize(file_name, width, height);
      
      return (HBITMAP)LoadImage(GetModuleHandle(NULL), file_name, IMAGE_BITMAP, *width, *height, LR_LOADFROMFILE );
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Unreg009
    Guest
    Code:
    d:\dev-c++\main.cpp: In function `LRESULT WndProc(HWND__ *, unsigned int, unsigned int, long int)':
    d:\dev-c++\main.cpp:164: redefinition of `LRESULT WndProc(HWND__ *, unsigned int, unsigned int, long int)'
    d:\dev-c++\main.cpp:16: `LRESULT WndProc(HWND__ *, unsigned int, unsigned int, long int)' previously defined here
    I get these errors when compiling with Dev C. I also got an error about IDB_BITMAP1 being undefined, even though it was defined in the resource file. Can anyone explain what I am doing wrong?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Those error messages typically mean you did this:

    Code:
    #define IDB_BITMAP1  1002 //..you forgot to put this line here.
    
    void foo() {} //..you probably did this.
    void foo();    //..when you meant to do this.
    
     //....rest of program...
    
    void foo() {
     int i;
     //..etc.
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    UnReg009
    Guest
    Right on both counts. =) Much thanks indeed!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. get bitmap from image in website.
    By sgh in forum C# Programming
    Replies: 3
    Last Post: 03-23-2009, 09:34 PM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. How to create and display a bitmap programatically?
    By solar3147 in forum Windows Programming
    Replies: 4
    Last Post: 06-24-2003, 02:55 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM