SelectObject error with bitmap

This is a discussion on SelectObject error with bitmap within the Windows Programming forums, part of the Platform Specific Boards category; SelectObject() is returning NULL when I try to display a bitmap. This is an MFC app, and I've written my ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    SelectObject error with bitmap

    SelectObject() is returning NULL when I try to display a bitmap. This is an MFC app, and I've written my own class that loads up a bitmap from a file. Here's some code.
    Code:
    //CBitmap.h
    #ifndef CBitmapH
    #define CBitmapH
    
    #include "stdafx.h"
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    class CBitmapEx
    {
    private:
    	HBITMAP hBitmapHandle;
    
    public:
    	CBitmapEx() {}
    	~CBitmapEx();
    
    	HBITMAP GetHandle(void) {return hBitmapHandle; }
    	void LoadBitmapEx(IN char *lpszFileName);
    
    	DWORD GetDword(IN ifstream &file);
    
    	stringstream strTemp;
    };
    
    #endif
    Code:
    //CBitmap.cpp
    #include "stdafx.h"
    #include "CBitmap.h"
    
    #include <sstream>
    #include <fstream>
    using namespace std;
    
    CBitmapEx::~CBitmapEx()
    {
    	DeleteObject(hBitmapHandle);
    }
    
    void CBitmapEx::LoadBitmapEx(char *lpszFileName)
    {
    	DWORD dwDataOffset;
    	DWORD dwWidth,dwHeight;
    	DWORD dwBitCount;
    	DWORD dwImageSize;
    
    	DWORD dwPadding;
    
    	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);
    
    	char *lpBits=new char[dwImageSize];
    
    	file.seekg(dwDataOffset);
    	for (DWORD i=0;i<dwImageSize;i++)
    	{
    		lpBits[i]=file.get();
    	}
    
    	hBitmapHandle=CreateBitmap(dwWidth,dwHeight, 1, dwBitCount ,(LPVOID)lpBits);
    	if (!hBitmapHandle)
    		MessageBox(NULL,"uh oh",NULL,NULL);
    
    	delete [] lpBits;
    	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:
    //In the ViewFrame constructor
    CBitmapEx cbe;
    cbe.LoadBitmapEx("c:\\a.bmp");
    
    //later, in the OnDraw function
    if (!pDC->SelectObject(cbe.GetHandle()))
            pDC->TextOut(0,0,"SelectObject() returned NULL");
    The TextOut() works fine, but obviously the SelectObject() doesn't. The bitmap is where it says it is, and it loads up the information properly. What I'm not sure of is if it loads up the actual bitmap data properly.

    Does anyone have any ideas about this?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,666
    From MSDN - CreateBitMap()
    After a bitmap is created, it can be selected into a device context by calling the SelectObject function.
    However, the bitmap can only be selected into a device context if the bitmap and the DC have the same format.
    You'll want to follow the techniques discussed in this article:
    DIBs and Their Use

    gg

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That article is great, everythings working fully now. Thanks codeplug.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 08:43 AM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  3. Loading a Bitmap resource for OpenGL Texture[x]
    By the dead tree in forum Game Programming
    Replies: 4
    Last Post: 08-26-2004, 01:12 PM
  4. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 09:07 AM
  5. texture is all white in opengl!
    By Crossbow in forum Game Programming
    Replies: 7
    Last Post: 03-31-2002, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21