CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 1999
    Posts
    2

    Urgent: I need help with text on bitmaps



    I need to take a CBitmap, write text on it,

    and then get it back into a CBitmap, what

    is the best way to do this? Right now I'm

    doing:

    CBitmap bitmap;

    bitmap.LoadBitmap(IDB_BITMAP1);

    //and then I create a CDC object:

    CDC dcObj;

    dcObj.CreateCompatibleDC(NULL);

    dcObj.SelectObject(bitmap);

    dcObj.TextOut(0,0, "Hi"

    // now how exactly do I get it back out?

    // I tried this:

    CBitmap bitmap2;

    bitmap2.CreateCompatibleBitmap(&dcObj, 10, 10);

    // and all I get is a black background with

    // a grey square in the middle which I think is 10x10

    I also tried using the CDC.SelectObject to try to

    get the bitmap out and I used GetCurrentBitmap, both

    gave me back a grey bitmap. So could someone please

    tell me how to do this, or maybe show me where I am

    going wrong in my code? Oh and I am pretty new to

    C++ and MFC so please speak slowly and with a lot of

    big hand movements

    Thanks



  2. #2
    Join Date
    May 1999
    Posts
    123

    Re: Urgent: I need help with text on bitmaps



    When you select your bitmap into the DC, save the DC that it started out with.

    CBitmap old_ddb = dcobj.SelectObject(bitmap);

    then do your drawing. When you're done, select the original bitmap back into the DC:

    dcobj.SelectObject(old_ddb);

    and simply use your bitmap. You don't have to do anything to get it back out of the DC -- when you select a bitmap

    into a DC, you're basically just telling the DC "here's a block of memory you can draw into". You don't have to get

    the bitmap back out, because it was never _in_ to start with...

    Here's a bit of tested code. This part is in an OnInitialUpdate:

    CClientDC cdc(this);

    dc.CreateCompatibleDC(&cdc);

    bm.CreateCompatibleBitmap(&cdc, 100, 100);

    dc.SelectObject(&bm);

    dc.TextOut(0,0, "Hi"

    Then this is added to the OnDraw:

    pDC->BitBlt(0, 0, 100, 100, &dc, 0, 0, SRCCOPY);

    and I get "Hi" in black on a white background, then a black box to the right and below that for a total of 100 pixels.



Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured