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. Save CBitmap object to disk

Save CBitmap object to disk

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
4 Posts 3 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.
  • C Offline
    C Offline
    capint
    wrote on last edited by
    #1

    I save a CBitmap object to disk with this code : CDC * pDC = GetDC(); CBitmap Bitmap; Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY); Image.Attach(Bitmap); Image.Save(_T("xxx.bmp")); but the bitmap I saved was completely black Some one plz help me :rose:

    R H 2 Replies Last reply
    0
    • C capint

      I save a CBitmap object to disk with this code : CDC * pDC = GetDC(); CBitmap Bitmap; Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY); Image.Attach(Bitmap); Image.Save(_T("xxx.bmp")); but the bitmap I saved was completely black Some one plz help me :rose:

      R Offline
      R Offline
      Rajkumar R
      wrote on last edited by
      #2

      capint wrote:

      Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY); Image.Attach(Bitmap);

      this creates only the bitmap object, allocates resources for the bitmap and the color format is compatible with the Device context, but the image it initially contains is blank, you need to fill the image in to the allocated bitmap resource. I assume you want the image in the Device context then you need to copy the bitmap from dc to this bitmap,

      CBitmap Bitmap;
      Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY);
      CDC dcMemory;
      dcMemory.CreateCompatibleDC(pDC);

      // Select the bitmap into the in-memory DC
      CBitmap* pOldBitmap = dcMemory.SelectObject(&Bitmap);

      // Copy the bits from the on-screen DC into the
      // in-memory DC
      dcMemory.BitBlt(0, 0, screenMaxX, screenMaxY, pDC,
      0, 0, SRCCOPY);

      dcMemory.SelectObject(pOldBitmap);

      C 1 Reply Last reply
      0
      • R Rajkumar R

        capint wrote:

        Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY); Image.Attach(Bitmap);

        this creates only the bitmap object, allocates resources for the bitmap and the color format is compatible with the Device context, but the image it initially contains is blank, you need to fill the image in to the allocated bitmap resource. I assume you want the image in the Device context then you need to copy the bitmap from dc to this bitmap,

        CBitmap Bitmap;
        Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY);
        CDC dcMemory;
        dcMemory.CreateCompatibleDC(pDC);

        // Select the bitmap into the in-memory DC
        CBitmap* pOldBitmap = dcMemory.SelectObject(&Bitmap);

        // Copy the bits from the on-screen DC into the
        // in-memory DC
        dcMemory.BitBlt(0, 0, screenMaxX, screenMaxY, pDC,
        0, 0, SRCCOPY);

        dcMemory.SelectObject(pOldBitmap);

        C Offline
        C Offline
        capint
        wrote on last edited by
        #3

        Thanks so much :D

        1 Reply Last reply
        0
        • C capint

          I save a CBitmap object to disk with this code : CDC * pDC = GetDC(); CBitmap Bitmap; Bitmap.CreateCompatibleBitmap(pDC,screenMaxX, screenMaxY); Image.Attach(Bitmap); Image.Save(_T("xxx.bmp")); but the bitmap I saved was completely black Some one plz help me :rose:

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          Saving a Drawing to a Bitmap File[^].

          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