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 to File

Save CBitmap to File

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
10 Posts 4 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.
  • D Offline
    D Offline
    Dhiraj kumar Saini
    wrote on last edited by
    #1

    I have a CBitmap object and want to save it as a file . Please help me how to achieve it. Thanks & regards. Dhiraj

    E C 2 Replies Last reply
    0
    • D Dhiraj kumar Saini

      I have a CBitmap object and want to save it as a file . Please help me how to achieve it. Thanks & regards. Dhiraj

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #2

      Starting from CBitmap (MFC) or a GDI HBITMAP (what MFC wraps) it's not so easy: A sample is on MSDN http://msdn.microsoft.com/en-us/library/ms532340(VS.85).aspx[^] Otherwise, cosider using GDI+, where all this stuffs are wrappen in the Gdiplus::Bitmap class that can be created from an HBITMAP. (but you have to link for GDI+ libraries and run startup/termination appropriately: just llok GDI+ under MSDN)

      2 bugs found. > recompile ... 65534 bugs found. :doh:

      1 Reply Last reply
      0
      • D Dhiraj kumar Saini

        I have a CBitmap object and want to save it as a file . Please help me how to achieve it. Thanks & regards. Dhiraj

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

        You may use CImage class [^], check out the CImage::Attach method. I suppose (I didn't test it) the following code will do the trick:

        CBitmap myBmp;
        // bitmap initialization code
        CImage myImg;
        myImg.Attach(myBmp);
        myImg.Save(_T("my.bmp"));
        myImg.Detach();

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        D 1 Reply Last reply
        0
        • C CPallini

          You may use CImage class [^], check out the CImage::Attach method. I suppose (I didn't test it) the following code will do the trick:

          CBitmap myBmp;
          // bitmap initialization code
          CImage myImg;
          myImg.Attach(myBmp);
          myImg.Save(_T("my.bmp"));
          myImg.Detach();

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          D Offline
          D Offline
          Dhiraj kumar Saini
          wrote on last edited by
          #4

          Its giving assertion related to size.

          C H 2 Replies Last reply
          0
          • D Dhiraj kumar Saini

            Its giving assertion related to size.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Dhiraj kumar Saini wrote:

            Its giving assertion related to size.

            This is not a good way of getting help. You should be more precise and detailed when reporting errors. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            1 Reply Last reply
            0
            • D Dhiraj kumar Saini

              Its giving assertion related to size.

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

              Did you trace it ? and can you show your code?

              D 1 Reply Last reply
              0
              • H Hamid Taebi

                Did you trace it ? and can you show your code?

                D Offline
                D Offline
                Dhiraj kumar Saini
                wrote on last edited by
                #7

                Ya sure.

                void CCaptureDesktopDlg::OnBnClickedButton1()
                {
                // TODO: Add your control notification handler code here

                CString sFName;
                CDC ScreenDC;
                ScreenDC.Attach(::GetDC(NULL));

                CBitmap Capture;
                CSize Dimensions(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
                Capture.CreateCompatibleBitmap(&ScreenDC, Dimensions.cx, Dimensions.cy);

                CDC MemDC;
                MemDC.CreateCompatibleDC(&ScreenDC);

                CBitmap *OldBitmap = MemDC.SelectObject(&Capture);

                MemDC.BitBlt(0, 0, Dimensions.cx, Dimensions.cy, &ScreenDC, 0, 0, SRCCOPY);

                //added on 28Nov08

                CFileDialog fd( FALSE, // true for open, false for save

                _T("BMP"), // default extention

                NULL, // initial filename in box);

                OFN_HIDEREADONLY & // flags for detailed behaviour

                OFN_OVERWRITEPROMPT,

                NULL, // file-filter pairs

                NULL); // pointer to parent window

                fd.m_ofn.lpstrTitle = _T("Enter file name...");

                fd.m_ofn.lpstrInitialDir = NULL;

                fd.m_ofn.lpstrFilter = _T("BMP files (*.bmp)\000*.BMP\000");

                if(fd.DoModal() == IDOK)

                sFName = fd.GetPathName();

                else

                return;

                HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL );

                BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib);

                if(!b)

                {

                CString sMess = _T("Unable to write to file: \n");

                sMess += sFName;

                AfxMessageBox(sMess);

                }

                MemDC.SelectObject(Capture);

                Capture.Detach(); // make sure bitmap not deleted with CBitmap object

                //----------------

                MemDC.SelectObject(OldBitmap);
                MemDC.DeleteDC();

                ::ReleaseDC(NULL, ScreenDC.Detach());

                }
                BOOL CCaptureDesktopDlg::WriteDIB( LPTSTR szFile, HANDLE hDIB)

                {

                BITMAPFILEHEADER hdr;

                LPBITMAPINFOHEADER lpbi;

                if (!hDIB)

                return FALSE;

                CFile file;

                if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )

                return FALSE;

                lpbi = (LPBITMAPINFOHEADER)hDIB;

                /* int nColors = 1 << lpbi->biBitCount;

                */

                int nColors = 1 << lpbi->biBitCount;

                if (nColors > 256)

                nColors = 0;

                // Fill in the fields of the file header

                hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"

                hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );

                hdr.bfReserved1 = 0;

                hdr.bfReserved2 = 0;

                //// hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +

                //// nColors * sizeof(RGBQUAD))

                H 1 Reply Last reply
                0
                • D Dhiraj kumar Saini

                  Ya sure.

                  void CCaptureDesktopDlg::OnBnClickedButton1()
                  {
                  // TODO: Add your control notification handler code here

                  CString sFName;
                  CDC ScreenDC;
                  ScreenDC.Attach(::GetDC(NULL));

                  CBitmap Capture;
                  CSize Dimensions(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
                  Capture.CreateCompatibleBitmap(&ScreenDC, Dimensions.cx, Dimensions.cy);

                  CDC MemDC;
                  MemDC.CreateCompatibleDC(&ScreenDC);

                  CBitmap *OldBitmap = MemDC.SelectObject(&Capture);

                  MemDC.BitBlt(0, 0, Dimensions.cx, Dimensions.cy, &ScreenDC, 0, 0, SRCCOPY);

                  //added on 28Nov08

                  CFileDialog fd( FALSE, // true for open, false for save

                  _T("BMP"), // default extention

                  NULL, // initial filename in box);

                  OFN_HIDEREADONLY & // flags for detailed behaviour

                  OFN_OVERWRITEPROMPT,

                  NULL, // file-filter pairs

                  NULL); // pointer to parent window

                  fd.m_ofn.lpstrTitle = _T("Enter file name...");

                  fd.m_ofn.lpstrInitialDir = NULL;

                  fd.m_ofn.lpstrFilter = _T("BMP files (*.bmp)\000*.BMP\000");

                  if(fd.DoModal() == IDOK)

                  sFName = fd.GetPathName();

                  else

                  return;

                  HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL );

                  BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib);

                  if(!b)

                  {

                  CString sMess = _T("Unable to write to file: \n");

                  sMess += sFName;

                  AfxMessageBox(sMess);

                  }

                  MemDC.SelectObject(Capture);

                  Capture.Detach(); // make sure bitmap not deleted with CBitmap object

                  //----------------

                  MemDC.SelectObject(OldBitmap);
                  MemDC.DeleteDC();

                  ::ReleaseDC(NULL, ScreenDC.Detach());

                  }
                  BOOL CCaptureDesktopDlg::WriteDIB( LPTSTR szFile, HANDLE hDIB)

                  {

                  BITMAPFILEHEADER hdr;

                  LPBITMAPINFOHEADER lpbi;

                  if (!hDIB)

                  return FALSE;

                  CFile file;

                  if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )

                  return FALSE;

                  lpbi = (LPBITMAPINFOHEADER)hDIB;

                  /* int nColors = 1 << lpbi->biBitCount;

                  */

                  int nColors = 1 << lpbi->biBitCount;

                  if (nColors > 256)

                  nColors = 0;

                  // Fill in the fields of the file header

                  hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"

                  hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );

                  hdr.bfReserved1 = 0;

                  hdr.bfReserved2 = 0;

                  //// hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +

                  //// nColors * sizeof(RGBQUAD))

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

                  Change to it: ;) if(fd.DoModal() == IDOK) sFName = fd.GetPathName(); else return; CImage m2; m2.Attach(Capture); m2.Save(fd.GetPathName()); m2.Detach(); /*HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL ); BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib); if(!b) { CString sMess = _T("Unable to write to file: \n"); sMess += sFName; AfxMessageBox(sMess); } */ MemDC.SelectObject(Capture); Capture.Detach();

                  D 1 Reply Last reply
                  0
                  • H Hamid Taebi

                    Change to it: ;) if(fd.DoModal() == IDOK) sFName = fd.GetPathName(); else return; CImage m2; m2.Attach(Capture); m2.Save(fd.GetPathName()); m2.Detach(); /*HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL ); BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib); if(!b) { CString sMess = _T("Unable to write to file: \n"); sMess += sFName; AfxMessageBox(sMess); } */ MemDC.SelectObject(Capture); Capture.Detach();

                    D Offline
                    D Offline
                    Dhiraj kumar Saini
                    wrote on last edited by
                    #9

                    Thanx its working.

                    H 1 Reply Last reply
                    0
                    • D Dhiraj kumar Saini

                      Thanx its working.

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

                      You're welcome. ;)

                      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