Solved

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

Posted on 2015-01-05
1
279 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
1 Comment
 
LVL 33

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 84
gdb doesn't stop on breakpoint 2 93
Question to Pivot table 1 63
Precision Problem in C++ 7 44
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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