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. Problems with CStatic GetBitmap

Problems with CStatic GetBitmap

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structureshelpquestion
4 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.
  • J Offline
    J Offline
    JJeffrey
    wrote on last edited by
    #1

    I am currently trying to get the bitarray of bitmap that has already been loaded into a picture box of a dialog window when I press a button on the dialog. The overall aim is, Button 1 takes data from user, takes editing parameters and creates a BMP from it the result of the editing , putting the BMP into a picture box in a dialog. Button 2 is the "save" feature, which allows the final changed Bitmap to be saved back into bitmapbits array. Here's the problem:

    void CProjectDlg::OnButton2Press()
    {
    CFile TestFile;
    HBITMAP CapturedBMP = m_PicBox.GetBitmap(); //------------ (1)
    CBitmap RS232BMP;
    RS232BMP.Attach(CapturedBMP);
    BITMAP TempBMStore;
    RS232BMP.GetBitmap(&TempBMStore); //------------- (2)
    BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, TempBMStore.bmWidthBytes*TempBMStore.bmHeight);
    DWORD dwValue=RS232BMP.GetBitmapBits(TempBMStore.bmWidthBytes*TempBMStore.bmHeight, bmpBuffer);
    }

    I get a valid handle at (1) but I my TempBMStore is blank after I execute line (2), meaning I do not have a valid Bitmap or something (return value is 0). What Am I doing wrong here? When I cut and paste this code into the section of code that originally places the bitmap into the picture box, I have absolutely no problem. ie:

    void CProjectDlg::OnButton1Press()
    {
    .
    .
    .
    .
    .
    .
    .
    .
    .
    m_PicBox.SetBitmap(TmpBmp);

    CFile	TestFile;
    HBITMAP CapturedBMP = m\_PicBox.GetBitmap();              //------------ (1)
    CBitmap	RS232BMP;
    RS232BMP.Attach(CapturedBMP);
    BITMAP TempBMStore;
    RS232BMP.GetBitmap(&TempBMStore);                       //------------- (2)
    BYTE\* bmpBuffer=(BYTE\*)GlobalAlloc(GPTR, TempBMStore.bmWidthBytes\*TempBMStore.bmHeight);	
    DWORD dwValue=RS232BMP.GetBitmapBits(TempBMStore.bmWidthBytes\*TempBMStore.bmHeight, bmpBuffer);
    

    }

    Thje return for line (2) is valid and I can get the bitmap bits with no errors. Same code, different behaviour. Do I need to make the Picture Box valid or something?

    Thanks in advance

    C 1 Reply Last reply
    0
    • J JJeffrey

      I am currently trying to get the bitarray of bitmap that has already been loaded into a picture box of a dialog window when I press a button on the dialog. The overall aim is, Button 1 takes data from user, takes editing parameters and creates a BMP from it the result of the editing , putting the BMP into a picture box in a dialog. Button 2 is the "save" feature, which allows the final changed Bitmap to be saved back into bitmapbits array. Here's the problem:

      void CProjectDlg::OnButton2Press()
      {
      CFile TestFile;
      HBITMAP CapturedBMP = m_PicBox.GetBitmap(); //------------ (1)
      CBitmap RS232BMP;
      RS232BMP.Attach(CapturedBMP);
      BITMAP TempBMStore;
      RS232BMP.GetBitmap(&TempBMStore); //------------- (2)
      BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, TempBMStore.bmWidthBytes*TempBMStore.bmHeight);
      DWORD dwValue=RS232BMP.GetBitmapBits(TempBMStore.bmWidthBytes*TempBMStore.bmHeight, bmpBuffer);
      }

      I get a valid handle at (1) but I my TempBMStore is blank after I execute line (2), meaning I do not have a valid Bitmap or something (return value is 0). What Am I doing wrong here? When I cut and paste this code into the section of code that originally places the bitmap into the picture box, I have absolutely no problem. ie:

      void CProjectDlg::OnButton1Press()
      {
      .
      .
      .
      .
      .
      .
      .
      .
      .
      m_PicBox.SetBitmap(TmpBmp);

      CFile	TestFile;
      HBITMAP CapturedBMP = m\_PicBox.GetBitmap();              //------------ (1)
      CBitmap	RS232BMP;
      RS232BMP.Attach(CapturedBMP);
      BITMAP TempBMStore;
      RS232BMP.GetBitmap(&TempBMStore);                       //------------- (2)
      BYTE\* bmpBuffer=(BYTE\*)GlobalAlloc(GPTR, TempBMStore.bmWidthBytes\*TempBMStore.bmHeight);	
      DWORD dwValue=RS232BMP.GetBitmapBits(TempBMStore.bmWidthBytes\*TempBMStore.bmHeight, bmpBuffer);
      

      }

      Thje return for line (2) is valid and I can get the bitmap bits with no errors. Same code, different behaviour. Do I need to make the Picture Box valid or something?

      Thanks in advance

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Is it possible that TmpBmp in your OnButton1Press is a local variable and when the method returns it destroys the bitmap so when you later try to access it you get an invalid handle? (CBitmap's destructor destroys the bitmap associated with it)

      > The problem with computers is that they do what you tell them to do and not what you want them to do. <

      J 1 Reply Last reply
      0
      • C Code o mat

        Is it possible that TmpBmp in your OnButton1Press is a local variable and when the method returns it destroys the bitmap so when you later try to access it you get an invalid handle? (CBitmap's destructor destroys the bitmap associated with it)

        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

        J Offline
        J Offline
        JJeffrey
        wrote on last edited by
        #3

        Never thought of it that way. Thought the Picture box would store the data so I can retrieve it later or allow another function to operate on it. If the data object is destroyed like that, all my functions would have to be tightly coupled together. and that would be rather wierd from an OO programming POV. You have anyway to retrieve a bitmap from a picture box object in a dialog AFTER the function that places it there destroys the CDC that was containing it? Jeffrey

        C 1 Reply Last reply
        0
        • J JJeffrey

          Never thought of it that way. Thought the Picture box would store the data so I can retrieve it later or allow another function to operate on it. If the data object is destroyed like that, all my functions would have to be tightly coupled together. and that would be rather wierd from an OO programming POV. You have anyway to retrieve a bitmap from a picture box object in a dialog AFTER the function that places it there destroys the CDC that was containing it? Jeffrey

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Well, make your CBitmap a member variable of your dialog class, then the bitmap will be existent while your dialog is up, or you could use the Detach method to "disconnect" the CBitmap and the HBITMAP but i don't know if the image control is possible to destroy the bitmap for you, pay attention or you could leak GDI resources.

          > The problem with computers is that they do what you tell them to do and not what you want them to do. <

          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