Problems with CStatic GetBitmap
-
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
-
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
Is it possible that
TmpBmp
in yourOnButton1Press
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. <
-
Is it possible that
TmpBmp
in yourOnButton1Press
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. <
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
-
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
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. <