I read a LOT of tutorials on bitmaps. I learned that RGBQuad is for 32 bit bitmaps and RGBTripple is for 24 bit bitmaps..

I'm planning on getting pixel information from a bitmap that can be of 3 types. 24bit, 32bit, 32bit with alpha (Transparent).

Thing is, I don't know how to determine what types of bitmaps I'm loading (24, 32, 32a).
I have to be able to draw these to a Window/DC.


This is what I have so far.. Can anyone point me in the right direction? Can I use LoadImage?

#include <windows.h>
#include <iostream>

using namespace std;


                            /** 24-Bit Bitmaps **/
class TBitmap
{
    private:
        HANDLE hFile;
        DWORD Written;
        BITMAPFILEHEADER bFileHeader;
        BITMAPINFOHEADER bInfoHeader;
        RGBTRIPLE *Image;

    public:
        void LoadBitmapFromFile(const char* FilePath)
        {
                            /** Open File **/
            hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);

                            /** Read Header **/

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

                            /** Read Image **/

            int imagesize = bInfoHeader.biWidth * bInfoHeader.biHeight;             //Math to Allocate memory for the inamge.
            Image = new RGBTRIPLE[imagesize];                                       //Create a new image. Array.
            ReadFile(hFile, Image, imagesize * sizeof(RGBTRIPLE), &Written, 0);     //Reads images.
            CloseHandle(hFile);                                                     //Close File Handle.
        }

                                    /** GetPixel Location **/

        RGBTRIPLE GetPixel(int X, int Y)
        {
            return Image[(bInfoHeader.biHeight - 1 - Y) * bInfoHeader.biWidth + X];  //BMP is upside-down due to the way screen co-ordinates work.
        }

                                    /** Set Pixel Colour **/

        RGBTRIPLE SetPixel(int X, int Y, RGBTRIPLE Colour)
        {
            Image[(bInfoHeader.biHeight - 1 - Y) * bInfoHeader.biWidth + X] = Colour;
        }
};


int main()
{
    TBitmap BMP;
    BMP.LoadBitmapFromFile("Untitled.bmp");

    cin.get();
}

Hmmm but I want to learn to handle bitmaps myself :S I'm writing a header file for bitmaps.. It's to be used with Bitmap Finding functions with screenshots and images and stuff.. Sorta like image recognition.

I figured out how to write bitmaps to a file and how to get bitmaps from a device context but for the life of me cannot figure out if it's a 24 bit/32bit/32a bit bmp.. and how to handle anything other than 24 bit.. I can work with 24 bit but don't see any tuts on the other two types or how to determine which is which.

Ok I think I have the hang of it but I'm not sure if my bitmaps/objects are getting freed :S Why? Because for loadfromfile I did this->Bmp =.. thats because bmp is a pointer to a bitmap no?

Yet when I do M.Freed and then M.Exists() it still says it exists :S

I wasn't sure if LoadFromFile should be void or HBitmap and return one..

//My tester..

#include "Bitmap.h"

using namespace std;

int main()
{
    Bitmap M;
    M.LoadBitmapFromFile("Untitled.bmp");
    int X = 0; int Y = 0;
    M.GetSize(X, Y);
    M.Free();
    cin.get();

    return 0;
}

//My class..

class Bitmap
{
    private:
        HBITMAP Bmp;
        HANDLE hFile;
        DWORD Written;
        BITMAPFILEHEADER bFileHeader;
        BITMAPINFOHEADER bInfoHeader;
        unsigned char* Image;

    public:
        Bitmap() : Bmp(0) {}
        Bitmap(HBITMAP hBmp) : Bmp(hBmp) {}
        Bitmap(Bitmap &Tbmp) : Bmp(Tbmp.Release()) {}
        ~Bitmap() {Free();}
        void Free() {if(Bmp){DeleteObject(Bmp);}}

        operator HBITMAP() {return Bmp;}
        bool Exists()
        {
            if (!this->Bmp)         //BMP is a handle pointing to a bitmap.
                return false;
            return true;
        }

        HBITMAP GetHandle()
        {
            return this->Bmp;
        }

        void operator = (Bitmap &Tbmp)
        {
            if (Tbmp.Bmp != Bmp)
            {
                Free();
                Bmp = Tbmp.Release();
            }
        }

/*        Bitmap& operator = (Bitmap& Tbmp)
        {
            if (this != &Tbmp)
            {
                Free();
                Bmp = Tbmp;
            }
            return *this;
        }
*/

        void LoadBitmapFromFile(const char* FilePath)
        {
            hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
            if (hFile == 0)
            {
                std::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 < 24)
            {
                CloseHandle(hFile);
                MessageBox(NULL, "The Image Loaded cannot be less than 24bits.", "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;

            //Create DibSection, Blit Window Data to the Bitmap.
            HDC memDC = CreateCompatibleDC(0);
            BYTE* memory = 0;
            this->Bmp = CreateDIBSection(memDC, &info, DIB_RGB_COLORS, (void**)&memory, 0, 0);
            DeleteDC(memDC);
            CloseHandle(hFile);
        }

        void GetSize(int &width, int &height)
        {
            BITMAP bm;
            GetObject(Bmp, sizeof(bm), &bm);
            width = bm.bmWidth;
            height = bm.bmHeight;
        }

        HBITMAP Release()
        {
            HBITMAP hNull = Bmp;
            Bmp = 0;
            return hNull;
        }
};
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.