CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Issue with playing metafile to device context then saving to bitmap. What I missing?

  1. #1
    Join Date
    Jun 2002
    Location
    Monte Carlo
    Posts
    129

    Issue with playing metafile to device context then saving to bitmap. What I missing?

    Long day. Sure I'm missing something simple. Anyway, I have a valid old school metafile (not enhanced metafile) in the form of a LPMETAFILEPICT. It's valid and everything looks good.

    What I'm trying to do is save it to a bitmap. On my first try, I play it to the device context and get a black box. OK, I read somewhere on google to play the metafile on to a white bitmap. I fill the bitmap w/ white and I still get a black box. This should be simple, and maybe I'm missing something dumb.

    Can anyone point me the right direction? (This is a console app, btw)

    Here's what I'm doing:

    Code:
                    HDC hdc =   CreateCompatibleDC(NULL); // NULL = Display DC.
    		CDC* pdc = CDC::FromHandle(hdc);
    		CRect r = new CRect(100, 170, 270, 230);
    
    		CDC memDC;
    		memDC.CreateCompatibleDC(pdc);
    		CBitmap bitmap;
    		bitmap.CreateCompatibleBitmap(pdc, r.Width(), r.Height());
    		CBitmap* pOldBmp = memDC.SelectObject(&bitmap);
    		memDC.FillSolidRect(r, RGB(255, 255, 255)); // Fill bitmap w/ white(?)
    		memDC.PlayMetaFile(pMetaPict->hMF); // my LPMETAFILEPICT
    		pdc->BitBlt(r.left, r.top, r.right - r.left, r.bottom - r.top, &memDC, r.left, r.top, SRCCOPY);
    		CImage img;
    		img.Attach((HBITMAP)bitmap.Detach());
    		img.Save(_T("C:\\savedBitmap.bmp"), Gdiplus::ImageFormatBMP);
    All I get is a black box.

  2. #2
    Join Date
    Mar 2006
    Posts
    151

    Re: Issue with playing metafile to device context then saving to bitmap. What I missi

    Hello,

    In the code you posted, you don't have anything which is actually drawing on the device context of the window, but only to DC's in memory. This looks like MFC code. Are you doing this in the WM_PAINT handler and bit-blitting from *pdc to the "paint" DC? (You said this is a console window so I'm not sure that part of the framework even applies.) Maybe you need to use GetWindowDC?

    If that all checks out here are some additional suggestions:

    1. Start small and work up by making sure you can draw a red slash on the screen, then a red slash on the memory DC which gets BitBlitted to the window DC, and so on building up to what you really want.
    2. Are you getting normal return values from the functions which return error indicators? Try GetLastError immediately after any failures (or even easier just put "$err,hr" in your watch window as you step through in the debugger).
    3. Perhaps it would help to switch from a console window to a windowed application.

  3. #3
    Join Date
    Jun 2002
    Location
    Monte Carlo
    Posts
    129

    Re: Issue with playing metafile to device context then saving to bitmap. What I missi

    Thanks for the reply and sorry for my long delay in answering. (I got pulled into another issue.)

    My goal isn't to draw the device context to a window -- my goal is to play a metafile to a device context IN MEMORY and then save/convert that device context (in memory) to a bitmap.

    No matter what I do, I continue to get a solid black square.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)