Thread: Noob question about GDI again.

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    Noob question about GDI again.

    NVM Figured it out.
    Last edited by abachler; 03-21-2009 at 05:03 PM.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    GAH, no I didn't. I tried this code, which came right form a tutorial, and it doesnt work either -

    Code:
      case WM_PAINT: 
       hdc = BeginPaint(hwnd, &ps);
       hdcMem = CreateCompatibleDC(hdc);
       g_hbmBall = CreateBitmap(640 , 480 , 1 , 24 , Display);
       hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);
       GetObject(g_hbmBall, sizeof(bm), &bm);
       BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
       SelectObject(hdcMem, hbmOld);
       DeleteObject(g_hbmBall);
       DeleteDC(hdcMem);
       EndPaint(hwnd, &ps);
       return 0;
    I hate losing source code, otherwise id copy and past my already working code from other projects.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try CreateCompatibleBitmap()

    but...

    Your code will just copy a blank image to the screen

    I think you want to create the mem DC at start,
    load an image into it (LoadImage()).
    use this mem DC to copy the image when a paint is required
    and only release these on close.

    I also suggest 'clearing' the current screen with something like FillRect()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    I already have the image data in the BYTE* Display buffer, LoadImage is irrelevant, I'm not trying to display a bitmap from a file. The image in Display is updated frequently, so I need to recreate the bitmap each time to make sure it displays the latest frame.

    So basically I have a RAW RGB 24 bits per pixel image, in Display[], and I need to get that image into a bitmap object, and GDI isn't being very cooperative.

    I am getting an image, but it is mangled, its more than just a simple 32/24 bit incompatibility because if I upconvert the image, it is still mangled.

    Current code used for the image -
    Code:
    		case WM_PAINT: 
    
    			hdc = BeginPaint(hwnd, &ps);
    			//hdc = GetDC(hwnd);
    			hdcMem = CreateCompatibleDC(NULL);
    
    			//bmi.bmiColors					= NULL;
    			bmi.bmiHeader.biWidth			= 640;
    			bmi.bmiHeader.biHeight			= 480;
    			bmi.bmiHeader.biBitCount		= 24;
    			bmi.bmiHeader.biPlanes			= 1;
    			bmi.bmiHeader.biSize			= 1228800;
    			bmi.bmiHeader.biSizeImage		= 0;
    			bmi.bmiHeader.biClrImportant	= 0;
    			bmi.bmiHeader.biClrUsed			= 0;
    			bmi.bmiHeader.biCompression		= BI_RGB;
    			bmi.bmiHeader.biXPelsPerMeter	= 1200;
    			bmi.bmiHeader.biYPelsPerMeter	= 1200;
    			
    
    			//g_hbmDisplay = CreateBitmap(640 , 480 , 1 , 24 , Display);
    			//g_hbmDisplay = CreateCompatibleBitmap(hdc , 640 , 480);
    			g_hbmDisplay = CreateDIBitmap(hdc , &bmi.bmiHeader , 0 , Display , &bmi , DIB_RGB_COLORS);
    			GetObjectA(g_hbmDisplay, sizeof(bm), &bm);
    
    			//GetDIBits(hdc , g_hbmDisplay , 0 , 480 , NULL , &bmi , DIB_RGB_COLORS);
    
    			
    			if(g_hbmDisplay == NULL){
    				Beep(1000 , 50);
    				//g_hbmDisplay = (HBITMAP)LoadImageA(hThis,"640x480.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    				//GetObjectA(g_hbmDisplay, sizeof(bm), &bm);
    				//g_hbmDisplay = CreateDIBitmap(hdcMem , &bm , CBM_INIT , Display , &bm , DIB_RGB_COLORS);  
    				}
    			
    			//hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmDisplay);
    
    			//GetObject(g_hbmDisplay, sizeof(bm), &bm);
    
    			
    			SetBitmapBits(g_hbmDisplay , 921600 , Display);
    			//SetDIBits(hdcMem , g_hbmDisplay , 0 , 480 , Display , &bmi , DIB_RGB_COLORS);
    			SelectObject(hdcMem , g_hbmDisplay);
    			BitBlt(hdc, 0, 0, 640, 480, hdcMem, 0, 0, SRCCOPY);
    
    			//SelectObject(hdcMem, hbmOld);
    
    			DeleteObject(g_hbmDisplay);
    			DeleteDC(hdcMem);
    			//DeleteDC(hdc)
    
    			EndPaint(hwnd, &ps);
    
    			return 0;
    Last edited by abachler; 03-22-2009 at 05:55 AM.

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Hmm... bmiHeader.biSize refers to the size of the BITMAPINFOHEADER struct... are you sure it's 1.2 MB? I thought it was more like 40 bytes.

    I think bmiHeader.biSizeImage is where that particular number should go, although I don't think you need to define it.

    You may as well ignore the biXPelsPerMeter and biYPelsPerMeter params, I think they're only relevant for printer DCs.

    In GDI nomenclature DIB dimensions are usually expressed with negative height (e.g. -480). This is a throwback to the OS/2 days.

    I'd forget about the SetBitmapBits call, stick with SetDIBits instead and the changes above.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Solved it, turns out it WAS a 32/24 conversion issue, I just messed up the conversion function.
    Last edited by abachler; 03-22-2009 at 12:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Real Noob question
    By Dark Greek in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 06:49 PM
  5. Noob Question
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2005, 04:36 AM