I need someone to check my bitmap class for me. I load a bitmap from a file but I cannot get it to draw to the specified window :S

I'm not sure if I'm loading it right or how to get the pixel data for BOTH 32 and 24 bit images.

class Bitmap
{
    private:
        int width;
        int height;

        HANDLE hFile;
        DWORD Written;
        BITMAPFILEHEADER bFileHeader;
        BITMAPINFOHEADER bInfoHeader;
        unsigned char* Pixels;

    protected:
        HDC DC;
        HBITMAP Image;

	public:
		Bitmap(int Width, int Height) : width(Width), height(Height)
		{
            BITMAPINFO Info;
            memset(&Info, 0, sizeof(BITMAPINFO));
            Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            Info.bmiHeader.biPlanes = 1;
            Info.bmiHeader.biBitCount = 32;
            Info.bmiHeader.biCompression = BI_RGB;
            Info.bmiHeader.biWidth = width;
            Info.bmiHeader.biHeight = -height;
            DC = CreateCompatibleDC(0);
            BYTE* Memory = 0;
            Image = CreateDIBSection(DC, &Info, DIB_RGB_COLORS, (void**)Memory, 0, 0);
            SelectObject(DC, Image);
		}

		Bitmap(int Width, int Height, string Data) : width(Width), height(Height)
		{
                  //Incomplete..
		}

        Bitmap(const char* FilePath)
        {
            hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
            if (hFile == 0)
            {
                cout<<"Error Bitmap Does Not Exist";
                //throw "Error";
                return;
            }

            ReadFile(hFile, &bFileHeader, sizeof(bFileHeader), &Written, 0);
            ReadFile(hFile, &bInfoHeader, sizeof(bInfoHeader), &Written, 0);

            if (bInfoHeader.biBitCount < 32)
            {
                CloseHandle(hFile);
                MessageBox(NULL, "The Image Loaded cannot be less than 32bits.", "Error", 0);
                return;
            }

            if (bFileHeader.bfType != 0x4D42)
            {
                CloseHandle(hFile);
                MessageBox(NULL, "The File Is Not A Bitmap.", "Error", 0);
                return;
            }

            //Dibsection Info
            BITMAPINFO info;
            info.bmiHeader = bInfoHeader;
            info.bmiHeader.biHeight = -info.bmiHeader.biHeight;

            //Create DibSection, Blit Window Data to the Bitmap.
            DC = CreateCompatibleDC(0);
            BYTE* memory = 0;
            Image = CreateDIBSection(DC, &info, DIB_RGB_COLORS, (void**)&memory, 0, 0);
            CloseHandle(hFile);
        }

		~Bitmap()
		{
            DeleteDC(DC);
            DeleteObject(Image);
		}

        HDC getDC(){return DC;}

        int GetWidth(){return width;}

        int GetHeight(){return height;}

        void GetSize(int& Width, int& Height){Width = width; Height = height;}


        void DrawBitmap(HWND window, HBITMAP hBmp)
        {
            RECT r;
            GetClientRect(window, &r);
            int xPos = r.right - r.left;
            int yPos = r.bottom - r.top;

            bFileHeader.bfType      = 0x4D42;
            bFileHeader.bfSize      = 0;
            bFileHeader.bfReserved1 = 0;
            bFileHeader.bfReserved2 = 0;
            bFileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

            bInfoHeader.biSize          = sizeof(bInfoHeader);
            bInfoHeader.biWidth         = xPos;
            bInfoHeader.biHeight        = yPos;
            bInfoHeader.biPlanes        = 1;
            bInfoHeader.biBitCount      = 24;
            bInfoHeader.biCompression   = BI_RGB;
            bInfoHeader.biSizeImage     = 0;
            bInfoHeader.biXPelsPerMeter = 0;
            bInfoHeader.biYPelsPerMeter = 0;
            bInfoHeader.biClrUsed       = 0;
            bInfoHeader.biClrImportant  = 0;

            HDC Bdc = CreateCompatibleDC(GetDC(window));    //Creates a DC for the source bitmap..
            HDC hdc = CreateCompatibleDC(0);
            if (SelectObject(hdc, hBmp) == 0)
            {
                DeleteDC(Bdc);
                return;
            }

            BITMAP Bms;
            if (!GetObject(hBmp, sizeof(Bms), &Bms))
            {
                DeleteDC(Bdc);
                return;
            }
            GetDIBits(Bdc, hBmp, 0, (UINT)Bms.bmHeight, NULL, (BITMAPINFO*) &bInfoHeader, DIB_RGB_COLORS);
            BitBlt(Bdc, 0, 0, xPos, yPos, hdc, 0, 0, SRCCOPY);
            DeleteDC(hdc);
            ReleaseDC(window, Bdc);
        }
};

Try declaring your HWND out of any functions or classes so everything can access it and in your class instead of HDC hdc, call HDC hdc =
GetDC(hWnd); Hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.