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 image from picturebox use MFC

Save image from picturebox use MFC

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicstutorialquestion
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.
  • J Offline
    J Offline
    josipahutar
    wrote on last edited by
    #1

    hi guys,,, i'm a newbie in MFC so i'm need your advice to complete final project about identification signature. in this time i have problem about how to save image from picture box in MFC. this is my code used in MFC. this code not save image from picturebox. what's wrong with my code??? void SaveBitmapToFile( BYTE* pBitmapBits, LONG lWidth, LONG lHeight,WORD wBitsPerPixel, LPCTSTR lpszFileName ); void CSaveimageDlg::OnSave() { // TODO: Add your control notification handler code here CDC *pDC = m_pic1.GetDC(); CDC dcMem1; CRect rect; m_pic1.GetClientRect(rect); HDC hdc; HBITMAP hBitmap = NULL; BITMAP bm; CBitmap Bitmap; m_pic1.SetBitmap(Bitmap); Bitmap.CreateBitmap(200 ,200,1,24,NULL); m_bmpBitmap.GetBitmap(&bm); //Bitmap.GetBitmap(&bm); Bitmap.GetObject( sizeof( BITMAP ), &bm ); unsigned char *pData = new unsigned char [bm.bmHeight*bm.bmWidthBytes]; SaveBitmapToFile(pData,bm.bmHeight,bm.bmWidth,24,(LPCTSTR)_T("a.bmp")); delete []pData; } void CSaveimageDlg::SaveBitmapToFile(BYTE *pBitmapBits, LONG lWidth, LONG lHeight, WORD wBitsPerPixel, LPCTSTR lpszFileName) { BITMAPINFOHEADER bmpInfoHeader = {0}; bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfoHeader.biBitCount = wBitsPerPixel; bmpInfoHeader.biClrImportant = 0; bmpInfoHeader.biClrUsed = 0; bmpInfoHeader.biCompression = BI_RGB; bmpInfoHeader.biHeight = lHeight; bmpInfoHeader.biWidth = lWidth; bmpInfoHeader.biPlanes = 1; bmpInfoHeader.biSizeImage = lWidth* lHeight * (wBitsPerPixel/8); BITMAPFILEHEADER bfh = {0}; bfh.bfType=0x4D42; bfh.bfType = 'B'+('M' << 8); bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage; HANDLE hFile = CreateFile( lpszFileName,GENERIC_WRITE, 0,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); if( !hFile ) // return if error opening file { return; } DWORD dwWritten = 0; WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL ); WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL ); WriteFile( hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwWritten, NULL ); CloseHandle( hFile ); }

    M 1 Reply Last reply
    0
    • J josipahutar

      hi guys,,, i'm a newbie in MFC so i'm need your advice to complete final project about identification signature. in this time i have problem about how to save image from picture box in MFC. this is my code used in MFC. this code not save image from picturebox. what's wrong with my code??? void SaveBitmapToFile( BYTE* pBitmapBits, LONG lWidth, LONG lHeight,WORD wBitsPerPixel, LPCTSTR lpszFileName ); void CSaveimageDlg::OnSave() { // TODO: Add your control notification handler code here CDC *pDC = m_pic1.GetDC(); CDC dcMem1; CRect rect; m_pic1.GetClientRect(rect); HDC hdc; HBITMAP hBitmap = NULL; BITMAP bm; CBitmap Bitmap; m_pic1.SetBitmap(Bitmap); Bitmap.CreateBitmap(200 ,200,1,24,NULL); m_bmpBitmap.GetBitmap(&bm); //Bitmap.GetBitmap(&bm); Bitmap.GetObject( sizeof( BITMAP ), &bm ); unsigned char *pData = new unsigned char [bm.bmHeight*bm.bmWidthBytes]; SaveBitmapToFile(pData,bm.bmHeight,bm.bmWidth,24,(LPCTSTR)_T("a.bmp")); delete []pData; } void CSaveimageDlg::SaveBitmapToFile(BYTE *pBitmapBits, LONG lWidth, LONG lHeight, WORD wBitsPerPixel, LPCTSTR lpszFileName) { BITMAPINFOHEADER bmpInfoHeader = {0}; bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfoHeader.biBitCount = wBitsPerPixel; bmpInfoHeader.biClrImportant = 0; bmpInfoHeader.biClrUsed = 0; bmpInfoHeader.biCompression = BI_RGB; bmpInfoHeader.biHeight = lHeight; bmpInfoHeader.biWidth = lWidth; bmpInfoHeader.biPlanes = 1; bmpInfoHeader.biSizeImage = lWidth* lHeight * (wBitsPerPixel/8); BITMAPFILEHEADER bfh = {0}; bfh.bfType=0x4D42; bfh.bfType = 'B'+('M' << 8); bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage; HANDLE hFile = CreateFile( lpszFileName,GENERIC_WRITE, 0,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); if( !hFile ) // return if error opening file { return; } DWORD dwWritten = 0; WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL ); WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL ); WriteFile( hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwWritten, NULL ); CloseHandle( hFile ); }

      M Offline
      M Offline
      mbue
      wrote on last edited by
      #2

      1.) width != byte_width! byte_width == (width*(bitsperpixel/8)+3)&~3 (4byte boundary) 2.) call GetBitmapBits to get the bitmap content before save. have a nice day.

      T J 2 Replies Last reply
      0
      • M mbue

        1.) width != byte_width! byte_width == (width*(bitsperpixel/8)+3)&~3 (4byte boundary) 2.) call GetBitmapBits to get the bitmap content before save. have a nice day.

        T Offline
        T Offline
        tagopi
        wrote on last edited by
        #3

        Hello Try this. http://www.ucancode.net/Visual\_C\_Codes/MFC-Example-CreateFile-WriteFile-save-memory-dc-bitmap-file.htm instead of memdc, you can call your picture box dc. Thanks A. Gopinath.

        J 1 Reply Last reply
        0
        • T tagopi

          Hello Try this. http://www.ucancode.net/Visual\_C\_Codes/MFC-Example-CreateFile-WriteFile-save-memory-dc-bitmap-file.htm instead of memdc, you can call your picture box dc. Thanks A. Gopinath.

          J Offline
          J Offline
          josipahutar
          wrote on last edited by
          #4

          thx for your answer sir.. but after i try the code, i can't find image c://temp.bmp. can u tell me , how to the code work in MFC to save a image di picturebox. my problem : i will load a image to picturebox in MFC then i'm want to save it. can u give sample code about my problem?? Regard's Johannes.

          1 Reply Last reply
          0
          • M mbue

            1.) width != byte_width! byte_width == (width*(bitsperpixel/8)+3)&~3 (4byte boundary) 2.) call GetBitmapBits to get the bitmap content before save. have a nice day.

            J Offline
            J Offline
            josipahutar
            wrote on last edited by
            #5

            Thx sir.. but i dont understand.. Can you explain in detail the program... Regard Johannes

            T 1 Reply Last reply
            0
            • J josipahutar

              Thx sir.. but i dont understand.. Can you explain in detail the program... Regard Johannes

              T Offline
              T Offline
              tagopi
              wrote on last edited by
              #6

              Hello try by sending this hDC to save. I didn't tried this. check whether it is working or not. HDC hDC=GetDC(GetDlgItem(IDC_PICTUREBOX); //------- id for your picture box. Save...... (hDC, ...); Thanks A. Gopinath.

              J 1 Reply Last reply
              0
              • T tagopi

                Hello try by sending this hDC to save. I didn't tried this. check whether it is working or not. HDC hDC=GetDC(GetDlgItem(IDC_PICTUREBOX); //------- id for your picture box. Save...... (hDC, ...); Thanks A. Gopinath.

                J Offline
                J Offline
                josipahutar
                wrote on last edited by
                #7

                sir, i got a error.. "GetDC' : function does not take 1 parameters" how about it??

                T 1 Reply Last reply
                0
                • J josipahutar

                  sir, i got a error.. "GetDC' : function does not take 1 parameters" how about it??

                  T Offline
                  T Offline
                  tagopi
                  wrote on last edited by
                  #8

                  Hello, sorry for delay. instead of HDC you can use CClientDC. that will work. Regards, A. Gopinath.

                  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