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. Create a CBitmap object, draw in it, save in (.bmp file)

Create a CBitmap object, draw in it, save in (.bmp file)

Scheduled Pinned Locked Moved C / C++ / MFC
comgraphicsdata-structureshelp
2 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.
  • R Offline
    R Offline
    Remi Morin
    wrote on last edited by
    #1

    in my Document class the function serialize... I wan,t to save my data as a monochrome bitmap so I do

    void CWoopDoc::Serialize(CArchive& ar)
    {
    if (ar.IsStoring())
    {
    //I need a bitmap object
    CBitmap BitMapTemp;
    //a device context to draw on it
    CDC CBitmapDC;
    CBitmap* OldBitMap;

      //initialisation of the dc
      CBitmapDC.CreateCompatibleDC(NULL);
    

    //here I'm not sure of that... I guess

      //I create an array of bytes for the bitmap
      void \* bitmapbitstemp = malloc( Size\_X\*Size\_Y / sizeof(BYTE));
      
      //here I create the bitmap
      BitMapTemp.CreateBitmap(Size\_X,Size\_Y,1,1,bitmapbitstemp);
    
    	//I send my bitmap to the DC and save the old one
      OldBitMap = CBitmapDC.SelectObject(&BitMapTemp);
     
     //(... I draw in it)
     
      //now I wan't to save it
      
      //I need a bitmapInfoHeader and A bitMap File Header
      BITMAPFILEHEADER BmpFileH;
      BITMAPINFOHEADER bmpInfoH;
      BITMAP bm;
      BitMapTemp.GetBitmap(&bm);     //<--here I'm unable the get the bitmap bits, but the creation return true!!!
      BmpFileH.biBitCount = bm.bmBitsPixel;
      BmpFileH.biCompression = BI\_RGB;
      BmpFileH.biHeight = bm.bmHeight;
      BmpFileH.biPlanes = bm.bmPlanes;
      BmpFileH.biWidth = bm.bmWidth;
      BmpFileH.biSizeImage = BI\_RGB;
      BmpFileH.biClrUsed = 0;
      BmpFileH.biClrImportant = 2;
      BmpFileH.biXPelsPerMeter = 0;
      BmpFileH.biYPelsPerMeter = 0;
      BmpFileH.biSize = sizeof(BmpFileH);
    
      bmpInfoH.bfOffBits = sizeof(bmpInfoH)+sizeof(BmpFileH);
      bmpInfoH.bfSize = bm.bmWidthBytes;
      bmpInfoH.bfType = ('M' << 8 | 'B');
      bmpInfoH.bfReserved1 = 0;
      bmpInfoH.bfReserved2 = 0;
      
      ar.Write(&BmpFileH,sizeof(BmpFileH));
      ar.Write(&bmpInfoH,sizeof(bmpInfoH));
      //here I need to write the byte them self... but bm.bmBits are always NULL
      ar.Write(bm.bmBits,bm.bmWidthBytes);  
      
      //destroying...
      CBitmapDC.SelectObject(OldBitMap);
    
      delete bitmapbitstemp;
    

    }
    else
    {
    // TODO: add loading code here
    }
    }

    BitMapTemp.Serialize(ar); //do not work... any help will be fine... Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

    B 1 Reply Last reply
    0
    • R Remi Morin

      in my Document class the function serialize... I wan,t to save my data as a monochrome bitmap so I do

      void CWoopDoc::Serialize(CArchive& ar)
      {
      if (ar.IsStoring())
      {
      //I need a bitmap object
      CBitmap BitMapTemp;
      //a device context to draw on it
      CDC CBitmapDC;
      CBitmap* OldBitMap;

        //initialisation of the dc
        CBitmapDC.CreateCompatibleDC(NULL);
      

      //here I'm not sure of that... I guess

        //I create an array of bytes for the bitmap
        void \* bitmapbitstemp = malloc( Size\_X\*Size\_Y / sizeof(BYTE));
        
        //here I create the bitmap
        BitMapTemp.CreateBitmap(Size\_X,Size\_Y,1,1,bitmapbitstemp);
      
      	//I send my bitmap to the DC and save the old one
        OldBitMap = CBitmapDC.SelectObject(&BitMapTemp);
       
       //(... I draw in it)
       
        //now I wan't to save it
        
        //I need a bitmapInfoHeader and A bitMap File Header
        BITMAPFILEHEADER BmpFileH;
        BITMAPINFOHEADER bmpInfoH;
        BITMAP bm;
        BitMapTemp.GetBitmap(&bm);     //<--here I'm unable the get the bitmap bits, but the creation return true!!!
        BmpFileH.biBitCount = bm.bmBitsPixel;
        BmpFileH.biCompression = BI\_RGB;
        BmpFileH.biHeight = bm.bmHeight;
        BmpFileH.biPlanes = bm.bmPlanes;
        BmpFileH.biWidth = bm.bmWidth;
        BmpFileH.biSizeImage = BI\_RGB;
        BmpFileH.biClrUsed = 0;
        BmpFileH.biClrImportant = 2;
        BmpFileH.biXPelsPerMeter = 0;
        BmpFileH.biYPelsPerMeter = 0;
        BmpFileH.biSize = sizeof(BmpFileH);
      
        bmpInfoH.bfOffBits = sizeof(bmpInfoH)+sizeof(BmpFileH);
        bmpInfoH.bfSize = bm.bmWidthBytes;
        bmpInfoH.bfType = ('M' << 8 | 'B');
        bmpInfoH.bfReserved1 = 0;
        bmpInfoH.bfReserved2 = 0;
        
        ar.Write(&BmpFileH,sizeof(BmpFileH));
        ar.Write(&bmpInfoH,sizeof(bmpInfoH));
        //here I need to write the byte them self... but bm.bmBits are always NULL
        ar.Write(bm.bmBits,bm.bmWidthBytes);  
        
        //destroying...
        CBitmapDC.SelectObject(OldBitMap);
      
        delete bitmapbitstemp;
      

      }
      else
      {
      // TODO: add loading code here
      }
      }

      BitMapTemp.Serialize(ar); //do not work... any help will be fine... Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

      B Offline
      B Offline
      Bill Wilson
      wrote on last edited by
      #2

      Are you sure you're drawing in the correct map? Using CBitMapDC->LineTo etc? It sounds as if there is nothing in BitMapTemp. Hope this helps, Bill

      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