Thread: Loading bitmaps with createfile() +CreateDIBSection

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    23

    Loading bitmaps with createfile() +CreateDIBSection

    Hi, I have been trying to learn windows graphics properly, so I can do more than just using brushes & stuff. The main thing I have been trying to do, is use CreateDIBSection, I think this isn't actually needed in loading bitmaps though and is just used to either create one from scratch, or get access to the bits in the bitmap. I may still want to use this function though as it could still be useful. What I mainly want to do (right now) though, is be able to load a bitmap with CreateFie(), and then, use the bitmap with bltbit etc. Also I need to be able to add other bitmaps to the same dc? I'm thinking of a gui, where if your mouse is over something it would highlight, -ie I woud "paste" another bitmap over that part.
    This is the code I've got so far for CreateFile() and CreateDIBSection.

    Code:
    switch(uiMessage)
    	{
    	case WM_CREATE:
    
    	FileHandle = CreateFile(afilename,GENERIC_READ | GENERIC_WRITE,
    		0,NULL,OPEN_EXISTING,0,NULL);
    
    	FileMapping = CreateFileMapping(FileHandle,NULL,PAGE_READWRITE,
    		0,0,NULL);
    
    	//MapViewOfFile returns a void pointer
    	FileHeader = (BITMAPFILEHEADER*)MapViewOfFile(FileMapping,
    		FILE_MAP_WRITE,0,0,0);
    
    	offset = FileHeader->bfOffBits;
    //	outfile <<FileHeader->bfOffBits;
    	return 0;
    
    	case WM_PAINT:
    	{
    			PAINTSTRUCT ps;
    			BITMAP bmp;	
    			HDC hdc = BeginPaint(hwnd, &ps);
    		
    			bm=CreateDIBSection(hdc,format,DIB_RGB_COLORS,(void**)&Bits,
    			FileMapping,offset);
    			
    			GetObject(FileMapping, sizeof(bm), &bm);
    
    			HDC memorydc=CreateCompatibleDC(hdc);
    
    			HBITMAP oldbitmap=(HBITMAP)SelectObject(memorydc,bm+offset);
    
    			BitBlt(hdc,0,0,32,32,memorydc,0,0,SRCCOPY);
    
    			SelectObject(memorydc,oldbitmap);
    
    			DeleteDC(memorydc);
    
    			EndPaint(hwnd, &ps);
    	return 0;
    Thanks for any help, like I said, I did want to use DIBSection, but at the end of the day I just want to be able to load/paste bitmaps, not as resources though.
    ps,
    C++

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    By a strange coincidence I've just been trying to display bitmaps, and here's what works for me:
    Code:
    //Bitmap loading code
    	ifstream file("c:\\a.bmp");
    
    	file.seekg(10);
    	dwDataOffset=GetDword(file);
    
    	file.seekg(18);
    	dwWidth=GetDword(file);
    
    	file.seekg(22);
    	dwHeight=GetDword(file);
    
    	file.seekg(28);
    	dwBitCount=GetDword(file);
    
    	file.seekg(34);
    	dwImageSize=GetDword(file);
    
    	dwPadding=4 - ((dwWidth*(dwBitCount/8)) % 4);
    
    	lpBits=new char[dwImageSize];
    
    	file.seekg(dwDataOffset);
    	for (DWORD i=0;i<dwImageSize;i++)
    	{
    		lpBits[i]=file.get();
    	}
    
    	strTemp << dwDataOffset << " "
    			<< dwWidth << " "
    			<< dwHeight << " "
    			<< dwBitCount << " "
    			<< dwPadding << " "
    			<< dwImageSize;
    	file.close();
    }
    
    DWORD CBitmapEx::GetDword(ifstream &file)
    {
    	char *lpTemp=new char[5];
    	DWORD dwTemp;
    
    	file.get(lpTemp,4);
    
    	dwTemp=*((DWORD *)lpTemp);
    
    	delete [] lpTemp;
    
    	return dwTemp;
    }
    Code:
    //Bitmap displaying code
    	BITMAPINFO *lpInfo=new BITMAPINFO;
    	lpInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    	lpInfo->bmiHeader.biWidth=cbe.dwWidth;
    	lpInfo->bmiHeader.biHeight=cbe.dwHeight;
    	lpInfo->bmiHeader.biPlanes=1;
    	lpInfo->bmiHeader.biBitCount=24;
    	lpInfo->bmiHeader.biCompression=BI_RGB;
    	
    	HBITMAP hBitmap=CreateCompatibleBitmap(pDC->m_hDC,cbe.dwWidth,cbe.dwHeight);
    	HDC hMemDC=CreateCompatibleDC(pDC->m_hDC);
    	SetDIBits(pDC->m_hDC,hBitmap,0,cbe.dwHeight,cbe.lpBits,lpInfo,DIB_RGB_COLORS);
    	hBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
    	BitBlt(pDC->m_hDC,5,5,lpInfo->bmiHeader.biWidth,lpInfo->bmiHeader.biHeight,
    				hMemDC,0,0,SRCCOPY);
    	DeleteObject(SelectObject(hMemDC,hBitmap));
    	DeleteDC(hMemDC);
    You'll have to play around with that a bit, but it's all there. Good luck.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Now that you two are here together...why not use LoadImage() or even LoadBitmap() instead of messing with the file yourself?

    gg

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Those two functions haven't worked out for me in the past. Anyway, reading in the file myself was a good exercise, I'm not really good at that sort of thing.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Now that you two are here together...why not use LoadImage() or even LoadBitmap() instead of messing with the file yourself?
    Heh, that's what I was going to say. Seems a lot easier to me... In fact, that's even how I load bitmaps in DirectDraw
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    23
    lol, LoadImage & LoadBitmap don't work for me either I don't know how to use resources!?

    Benny thanks for that, I will take a look at it. The CreateFile, CreateFileMapping and MapViewOfFile do exactly (I think) , the same thing, -returns a pointer to the start of the file, except with this, windows looks after the memory management etc.

    I was having a problem trying to get to the Header files, how to read them,- where are they? but I think I've got it now.
    Do you have to do the SetDIBits part? My book "Graphics programming in C++" (for windows) -Mark Walmsley, doesn't have that in it at all, it does say though, that the last two params of CreateDiBSection, "cause the file image bits to be incorporated into the new bitmap".- I presume that would mean the file you have just loaded?

    In case you might find it useful CreateDiBSection() allows you to get a pointer to the actual pixels, so you can change them/do things with them.

    On the subject of loading/reading files, does this mean you could do something similar with any type of file? I got really confused with the whole bit, but being able to read files like this could be quite useful. Have you any tips on the best ways to do it.
    Last edited by Stevo; 05-13-2004 at 03:48 PM.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "avatar.bmp", IMAGE_BITMAP,
                                        0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    if (hBmp == NULL)
    {
        // Handle error
    }//if
    
    // hBmp can now be selected into a compatible memory DC and bit-blit'd
    gg

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I don't know how to use resources!?
    You don't need to - see Codeplug's code above. But if you want to, you insert a Bitmap resource, then save the script file (go save all), then #include "resource.h"... then where it asks for a resource put MAKEINTRESOURCE(*whatever the resource was called*).

    *Edit: That's how it works in MSVC, I don't know how you would do it in other IDE's (or how you would do it manually for that matter You can look it up though, should be a 2-second google.)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Loading Bitmaps in Allegro
    By LiNeAr in forum Game Programming
    Replies: 1
    Last Post: 08-15-2005, 04:12 PM
  2. MapViewOfFile and CreateDIBSection problem with bitmaps
    By LuckY in forum Windows Programming
    Replies: 2
    Last Post: 08-18-2004, 08:37 AM
  3. Loading bitmaps without win32
    By /Muad'Dib\ in forum C++ Programming
    Replies: 0
    Last Post: 04-05-2004, 02:47 PM
  4. Loading Bitmaps
    By Neandrake in forum Windows Programming
    Replies: 4
    Last Post: 12-17-2001, 01:50 AM
  5. Loading Resource Bitmaps
    By Xterria in forum Windows Programming
    Replies: 10
    Last Post: 11-11-2001, 01:12 PM