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. 0 bytes CImage saved

0 bytes CImage saved

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicshelpquestion
8 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    I intend to save CBitmap into a bmp file, here is the code:

    // MyDocument.cpp
    CMyView* pView = (CMyView*)GetNextView(pos);
    CDC* pDC = pView->GetDC();

    		CDC MemDC;
    		MemDC.CreateCompatibleDC(pDC);
    
    		CBitmap bitmap;
    		bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy());
    		CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap);
    		// draw the MemDC
    		pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY);
    		CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap);
    		CImage Image;
    		Image.Attach((HBITMAP)pFinalBitmap->m\_hObject);
    		Image.Save(ar.GetFile()->GetFileName(), Gdiplus::ImageFormatBMP);
    		bitmap.DeleteObject();
    

    but the resulting bitmap file has 0 bytes ... why ? Could you help me ?

    CPalliniC J 2 Replies Last reply
    0
    • _ _Flaviu

      I intend to save CBitmap into a bmp file, here is the code:

      // MyDocument.cpp
      CMyView* pView = (CMyView*)GetNextView(pos);
      CDC* pDC = pView->GetDC();

      		CDC MemDC;
      		MemDC.CreateCompatibleDC(pDC);
      
      		CBitmap bitmap;
      		bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy());
      		CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap);
      		// draw the MemDC
      		pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY);
      		CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap);
      		CImage Image;
      		Image.Attach((HBITMAP)pFinalBitmap->m\_hObject);
      		Image.Save(ar.GetFile()->GetFileName(), Gdiplus::ImageFormatBMP);
      		bitmap.DeleteObject();
      

      but the resulting bitmap file has 0 bytes ... why ? Could you help me ?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      How are defined GetTotalCx, GetTotalCy?

      In testa che avete, signor di Ceprano?

      _ 2 Replies Last reply
      0
      • CPalliniC CPallini

        How are defined GetTotalCx, GetTotalCy?

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        They have the bitmap sizes, and they have valid values: 1400X1000. (I verified that). P.S. Moreover, if I put this code on OnDraw handler, is drawing well.

        1 Reply Last reply
        0
        • CPalliniC CPallini

          How are defined GetTotalCx, GetTotalCy?

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          Seem to go now, I overload CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace), and I put there the saving code ... In case that everyone need it. Thank you for all support.

          1 Reply Last reply
          0
          • _ _Flaviu

            I intend to save CBitmap into a bmp file, here is the code:

            // MyDocument.cpp
            CMyView* pView = (CMyView*)GetNextView(pos);
            CDC* pDC = pView->GetDC();

            		CDC MemDC;
            		MemDC.CreateCompatibleDC(pDC);
            
            		CBitmap bitmap;
            		bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy());
            		CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap);
            		// draw the MemDC
            		pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY);
            		CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap);
            		CImage Image;
            		Image.Attach((HBITMAP)pFinalBitmap->m\_hObject);
            		Image.Save(ar.GetFile()->GetFileName(), Gdiplus::ImageFormatBMP);
            		bitmap.DeleteObject();
            

            but the resulting bitmap file has 0 bytes ... why ? Could you help me ?

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            I don't think that it is the source of your problem, but there is an error in your code: You are deleting the CBitmap while it is still attached to the CImage. So you should detach first or use CImage::Destroy which will detach and delete the bitmap. You should also check the return value of the Save function.

            _ 1 Reply Last reply
            0
            • J Jochen Arndt

              I don't think that it is the source of your problem, but there is an error in your code: You are deleting the CBitmap while it is still attached to the CImage. So you should detach first or use CImage::Destroy which will detach and delete the bitmap. You should also check the return value of the Save function.

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #6

              CDC* pDC = pView->GetDC();
              int nSaveDC = pDC->SaveDC();

              CDC MemDC;
              MemDC.CreateCompatibleDC(pDC);
              
              CBitmap bitmap;
              bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy());
              CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap);
              // draw MemDC
              pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY);
              CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap);
              CImage Image;
              Image.Attach((HBITMAP)pFinalBitmap->m\_hObject);
              Image.Save(lpszPathName, Gdiplus::ImageFormatBMP);
              Image.Detach();
              pDC->RestoreDC(nSaveDC);
              bitmap.DeleteObject();
              

              Here is the new code ... but strange, CImage::Save return 0... it is OK ? The saved image looks good ...

              J 1 Reply Last reply
              0
              • _ _Flaviu

                CDC* pDC = pView->GetDC();
                int nSaveDC = pDC->SaveDC();

                CDC MemDC;
                MemDC.CreateCompatibleDC(pDC);
                
                CBitmap bitmap;
                bitmap.CreateCompatibleBitmap(pDC, GetTotalCx(), GetTotalCy());
                CBitmap\* pOldBitmap = (CBitmap\*)MemDC.SelectObject(&bitmap);
                // draw MemDC
                pDC->BitBlt(0, 0, GetTotalCx(), GetTotalCy(), &MemDC, 0, 0, SRCCOPY);
                CBitmap\* pFinalBitmap = (CBitmap\*)MemDC.SelectObject(pOldBitmap);
                CImage Image;
                Image.Attach((HBITMAP)pFinalBitmap->m\_hObject);
                Image.Save(lpszPathName, Gdiplus::ImageFormatBMP);
                Image.Detach();
                pDC->RestoreDC(nSaveDC);
                bitmap.DeleteObject();
                

                Here is the new code ... but strange, CImage::Save return 0... it is OK ? The saved image looks good ...

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #7

                CImage::Save returns a HRESULT where zero indicates success.

                _ 1 Reply Last reply
                0
                • J Jochen Arndt

                  CImage::Save returns a HRESULT where zero indicates success.

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #8

                  Ahh, yes, you are right. Thank you.

                  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