Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem in attaching CBitmap to CImage's Attach()

Problem in attaching CBitmap to CImage's Attach()

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humais
    wrote on last edited by
    #1

    i'm trying to attach CBitmap to CImage,but itz not working. /* CBitmap bmpCap; CBitmap *pBmpOld; CImage img; these three r declared in CChildView class */ //following code is in OnPaint() function...

    CRect rect;
    CDC dcMem;
    dc.TextOutA(20,20,"Humais");
    GetClientRect(&rect);
    bmpCap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
    pBmpOld=dc.SelectObject(&bmpCap);

    i'm getting runtime error...

    N 1 Reply Last reply
    0
    • H humais

      i'm trying to attach CBitmap to CImage,but itz not working. /* CBitmap bmpCap; CBitmap *pBmpOld; CImage img; these three r declared in CChildView class */ //following code is in OnPaint() function...

      CRect rect;
      CDC dcMem;
      dc.TextOutA(20,20,"Humais");
      GetClientRect(&rect);
      bmpCap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
      pBmpOld=dc.SelectObject(&bmpCap);

      i'm getting runtime error...

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      I don't know what you are trying to do here, but to answer your question

      img.Attach(bmpCap.Detach());
      // do some painting with img
      bmpCap.Attach(img.Detach());

      pBmpOld should be a local variable. Probably img as well.

      home

      H 1 Reply Last reply
      0
      • N Niklas L

        I don't know what you are trying to do here, but to answer your question

        img.Attach(bmpCap.Detach());
        // do some painting with img
        bmpCap.Attach(img.Detach());

        pBmpOld should be a local variable. Probably img as well.

        home

        H Offline
        H Offline
        humais
        wrote on last edited by
        #3

        i'm drawing shapes on CPaintDC dc....then i want to attach those shapes to img...so i can save them... can u please tell me proper steps?

        N 1 Reply Last reply
        0
        • H humais

          i'm drawing shapes on CPaintDC dc....then i want to attach those shapes to img...so i can save them... can u please tell me proper steps?

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #4

          This would be close (using only local variables)

          CRect rect;
          GetClientRect(&rect);

          CBitmap bmpCap;
          bmpCap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
          CBitmap *pBmpOld = dc.SelectObject(&bmpCap);

          dc.TextOutA(20,20,"Humais");

          dc.SelectObject(pBmpOld);

          CImage img;
          img.Attach(bmpCap.Detach());

          img.Save(...);

          bmpCap.Attach(img.Detach());

          ...

          home

          H 1 Reply Last reply
          0
          • N Niklas L

            This would be close (using only local variables)

            CRect rect;
            GetClientRect(&rect);

            CBitmap bmpCap;
            bmpCap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
            CBitmap *pBmpOld = dc.SelectObject(&bmpCap);

            dc.TextOutA(20,20,"Humais");

            dc.SelectObject(pBmpOld);

            CImage img;
            img.Attach(bmpCap.Detach());

            img.Save(...);

            bmpCap.Attach(img.Detach());

            ...

            home

            H Offline
            H Offline
            humais
            wrote on last edited by
            #5

            i'm using save function,thats why i declare CImage & CBitmap in CChildView class..tell me now where i have to put

            img.Attach(bmpCap.Detach());

            i mean in save function or in OnPaint function??

            N 1 Reply Last reply
            0
            • H humais

              i'm using save function,thats why i declare CImage & CBitmap in CChildView class..tell me now where i have to put

              img.Attach(bmpCap.Detach());

              i mean in save function or in OnPaint function??

              N Offline
              N Offline
              Niklas L
              wrote on last edited by
              #6

              In save. You have the bitmap in bmpCap, so you only have to move the handle over to the CImage instance while saving, and then move it back again. Make sure you're not creating the bitmap every time you enter OnPaint (should that not be OnDraw btw?). Verify that (HBITMAP)bmpCap == nullptr before creating it.

              home

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups