Solved

C++ / MFC, rendering to a local CPaintDC and saving to file

Posted on 2015-01-05
1
292 Views
Last Modified: 2015-02-09
So, as I said in an earlier post I've been trying to write some MFC C++ code to (1) create a CPaintDC, (2) render to it, and (3) save it to file as a BMP or DIB.  And I haven't been able to get this to work, surprisingly.

The code I've cobbled together from doc and samples is below, but for some reason it just outputs a black/null square.  (I should be drawing a green square.)

Can anyone see any problems with this code?

Thanks *very* much for any help.


void CMFCApplication_TestDIBDlg::OnBnClickedButton1()
{
    CPaintDC dc(this);
    CRect rc;
    GetClientRect(rc);
    dc.FillSolidRect(rc, 0x0000FF00);  // aarrggbb ; green

    HBITMAP m_hBmp;
    LPBYTE pBits;

    BITMAPINFO bmi;
    memset(&bmi, 0, sizeof(BITMAPINFO));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = rc.Width();
    bmi.bmiHeader.biHeight = -rc.Height(); // top-down
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;

    m_hBmp = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, (void**)&pBits, NULL, NULL);

    CImage image;
    image.Attach(m_hBmp);
    image.Save(L"c:\\dum.bmp", Gdiplus::ImageFormatBMP);

    MessageBox(L"Done", L"debug", 0);
}

Open in new window

0
Comment
Question by:RonMexico
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 40535336
the main problem of your code above is that the BITMAPINFO is sized with 1 RGBQUAD element only:

typedef struct tagBITMAPINFO {
  BITMAPINFOHEADER bmiHeader;
  RGBQUAD          bmiColors[1];
} BITMAPINFO, *PBITMAPINFO;

Open in new window


the structure is a c struct and not c++. to support dynamical bitmap sizes you need to allocate additional memory such that the bmiColors array is properly sized:

size_t sizBmiColors = rc.Width()*-rc.Height();
BITMAPINFO * pbmi = (BITMAPINFO *) new char[sizeof(BITMAPINFOHEADER) + sizBmiColors];
memset(pbmi, 0, sizeof(BITMAPINFOHEADER) + sizBmiColors);
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
...

Open in new window


Sara
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question