xcdemon05
xcdemon05

Reputation: 1422

Draw a bitmap directly into dialog box?

I have the BITMAPFILEHEADER, BITMAPINFOHEADER, and the image data of a bitmap stored in variables named, say, bf, bi, and imageData, respectively. When I output these to a .bmp file, I can open it without problem and view the image. I can also write some code to import this bmp file and display it in my dialog box.

My question is: is there an easy way to cut out the middle man and take my BITMAPFILEHEADER, BITMAPINFOHEADER, and image data and directly write these data into a 24 bitmap image displayed in the dialog box, without the need of using an external file?

For reference, here is the code I'm using now to output to a file, then input this file into the dialog box:

    //fileheader
    BITMAPFILEHEADER* bf = new BITMAPFILEHEADER;
    bf->bfType = 0x4d42;
    bf->bfSize = 6054400 + 54;
    bf->bfOffBits = 54;

    //infoheader
    BITMAPINFOHEADER* bi = new BITMAPINFOHEADER;
    bi->biSize = 40;
    bi->biWidth = 2752;
    bi->biHeight = -733;
    bi->biPlanes = 1;
    bi->biBitCount = 24;
    bi->biCompression = 0;
    bi->biSizeImage = 6054400;
    bi->biXPelsPerMeter = 2835;
    bi->biYPelsPerMeter = 2835;
    bi->biClrUsed = 0;
    bi->biClrImportant = 0;

    //image data
    unsigned char* imageData = new unsigned char[6054400];
    pFrame->GetImage(imageData);


    //////////////////////////////////////////////////////////////////////////
    //////////  Output to .bmp File   ////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////


    std::ofstream outFile;

    outFile.open("test.bmp", std::ios::binary|std::ios::out);

    outFile.write(reinterpret_cast<char *>(bf), sizeof(BITMAPFILEHEADER));
    outFile.write(reinterpret_cast<char *>(bi), sizeof(BITMAPINFOHEADER));
    outFile.write(reinterpret_cast<char *>(imageData), 6054400);

    outFile.close();

    //////////////////////////////////////////////////////////////////////////
    //////////  input .bmp and print to dialog box   /////////////////////////
    //////////////////////////////////////////////////////////////////////////

    CString szFilename ("C:/dev/NanoDev/TestApps/AVT_Testapp/test.bmp");
    HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, szFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    CBitmap bmp;
    bmp.Attach(hBmp);
    CClientDC dc(this);
    CDC bmDC;
    bmDC.CreateCompatibleDC(&dc);
    CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
    BITMAP  bi;
    bmp.GetBitmap(&bi);
    dc.BitBlt(384,26,bi.bmWidth/3,bi.bmHeight,&bmDC,0,0,SRCCOPY);
    bmDC.SelectObject(pOldbmp);

Upvotes: 1

Views: 1943

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308101

You can use the StretchDIBits function to draw directly from memory to a DC. You'll just need to convert your data from a BITMAPINFOHEADER to a BITMAPINFO structure.

Upvotes: 3

Related Questions