Solved

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

Posted on 2015-01-05
1
267 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 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now