Thread: Getting a Bitmap's Buffer

  1. #1
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341

    Getting a Bitmap's Buffer

    Is there a way to get the buffer from an HBITMAP object? I don't want to write my own bitmap loading function if there's a couple functions I can call. I start search. (I know. Search the forums first. Post code. I'm working on it) [edit]Actually I don't really have any forums to search so I don't feel to bad[/edit]
    Don't quote me on that... ...seriously

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Ok, I found this function:
    Code:
    LONG GetBitmapBits(
      HBITMAP hbmp,      // handle to bitmap
      LONG cbBuffer,     // number of bytes to copy
      LPVOID lpvBits     // pointer to buffer to receive bits
    );
    I can't find a function to get the size of the bitmaps. I know there should be something about the bitmap header.

    [edit]Found it (I think)
    Code:
    BOOL GetBitmapDimensionEx(
      HBITMAP hBitmap,     // handle to bitmap
      LPSIZE lpDimension   // address of structure receiving dimensions
    );
    [/edit]
    Don't quote me on that... ...seriously

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I'm either using it wrong or it doesn't work like I expect it to.
    Code:
    #define STRICT
    #include <windows.h>
    #include <stdio.h>
    
    
    int pascal WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance,
    				   LPSTR lpszCmdParam, int nCmdShow)
    {
    	HBITMAP bmp;
    	SIZE bmp_size;
    	char string[100];
    
    	bmp = LoadBitmap(hInst, "TestBmp");
    	if (!bmp)
    	{
    		MessageBox(0, "Failure to load bitmap", "Fatal Error", MB_OK | MB_ICONEXCLAMATION);
    		return 0;
    	}
    
    	if (!GetBitmapDimensionEx(bmp, &bmp_size))
    	{
    		MessageBox(0, "Failure to get bitmap dimensions", "Fatal Error", MB_OK | MB_ICONEXCLAMATION);
    		return 0;
    	}
    
    	sprintf(string, "Width: %d\nHeight:%d", bmp_size.cx, bmp_size.cy);
    
    	MessageBox(0, string, "Info", MB_OK | MB_ICONINFORMATION);
    
    	return 0;
    }
    [edit]Actually, I already know the dimensions cause I'm the one who made the bitmap. :-D. Hopefully the pitch isn't different than the width.[/edit]
    Last edited by Brad0407; 03-24-2007 at 08:58 PM.
    Don't quote me on that... ...seriously

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Three things:
    1. GetBitmapDimensionEx says that
    The GetBitmapDimensionEx function retrieves the dimensions of a compatible bitmap. The retrieved dimensions must have been set by the SetBitmapDimensionEx function.
    2. You must delete the HBITMAP handle with DeleteObject.
    3. If you want to get the bitmap dimmensions, you can try GetObject:
    Code:
    BITMAP tagbmp;
    GetObject(bmp, sizeof(BITMAP), &tagbmp);
    
    wsprintf(string, "W: %i\nH: %i", tagbmp.bmWidth, tagbmp.bmHeight);
    MessageBox(0, string, 0, 0);
    DeleteObject(bmp);
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Of course! I knew there was something like that I just could remember what it was. Thank you.
    Don't quote me on that... ...seriously

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    tagbmp.bmBits is 0. Any ideas?
    Don't quote me on that... ...seriously

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    ok, I've got this code. It gets the job done, but it's a little sloppy. Oh well:
    Code:
    #define STRICT
    #include <windows.h>
    #include <windowsx.h>
    #include <stdio.h>
    
    
    int pascal WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance,
    				   LPSTR lpszCmdParam, int nCmdShow)
    {
    	HBITMAP bmp;
    	BITMAP tagbmp;
    	HDC main_dc, bitmap_dc;
    	UINT color;
    	UCHAR r, g, b;
    	char string[100];
    
    	bmp = LoadBitmap(hInst, "TestBmp");
    	if (!bmp)
    	{
    		MessageBox(0, "Failure to load bitmap", "Fatal Error", MB_OK | MB_ICONEXCLAMATION);
    		return 0;
    	}
    
    	GetObject(bmp, sizeof(BITMAP), &tagbmp);
    
    	main_dc = GetDC(0);
    
    	bitmap_dc = CreateCompatibleDC(main_dc);
    	HBITMAP old_bmp = SelectBitmap(bitmap_dc, bmp);
    
    	color = GetPixel(bitmap_dc, 0, 0);
    
    	SelectBitmap(bitmap_dc, old_bmp);
    	DeleteDC(bitmap_dc);
    	DeleteObject(bmp);
    
    	r = (color >> 0) & 0xFF;
    	g = (color >> 8) & 0xFF;
    	b = (color >> 16) & 0xFF;
    	sprintf(string, "r: %d\ng: %d\nb: %d", r, g, b);
    	MessageBox(0, string, 0, 0);
    
    	return 0;
    }
    Last edited by Brad0407; 03-24-2007 at 10:37 PM.
    Don't quote me on that... ...seriously

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  9. #9
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Given this structure:
    Code:
    typedef struct TAGIMAGE
    {
    	int m_width;
    	int m_height;
    	UINT *m_buffer;
    } IMAGE, *LPIMAGE;
    These two functions should give me the same result, correct?
    Code:
    bool CreateImage(LPIMAGE image, HINSTANCE hInst, char *res)
    {
    	HBITMAP hbmp;
    	BITMAP bmp;
    
    	hbmp = LoadBitmap(hInst, res);
    	if (!hbmp)
    		return false;
    
    	GetObject(hbmp, sizeof(BITMAP), &bmp);
    	
    	image->m_width = bmp.bmWidth;
    	image->m_height = bmp.bmHeight;
    
    	image->m_buffer = (UINT*)GetBitmapPixels(hbmp, GetDC(0));
    	if (image->m_buffer == NULL)
    		return false;
    	return true;
    }
    Code:
    bool CreateImage(LPIMAGE image, HINSTANCE hInst, char *res)
    {
    	HBITMAP hbmp;
    	BITMAP bmp;
    	HDC main_dc, bitmap_dc;
    	
    	hbmp = LoadBitmap(hInst, res);
    	if (!hbmp)
    		return false;
    
    	GetObject(hbmp, sizeof(BITMAP), &bmp);
    	
    	image->m_width = bmp.bmWidth;
    	image->m_height = bmp.bmHeight;
    
    	main_dc = GetDC(0);
    	bitmap_dc = CreateCompatibleDC(main_dc);
    	HBITMAP old_bmp = SelectBitmap(bitmap_dc, hbmp);
    
    	image->m_buffer = new UINT[image->m_width * image->m_height];
    	for (int y = 0; y < image->m_height; y++)
    		for (int x = 0; x < image->m_width; x++)
    			image->m_buffer[y * image->m_width + x] = GetPixel(bitmap_dc, x, y);
    
    	SelectBitmap(bitmap_dc, old_bmp);
    	DeleteDC(bitmap_dc);
    	DeleteObject(hbmp);
    
    	return true;
    }
    The second works but the first doesn't.
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM