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?