Unselecting a bitmap in a context
-
Guys, I am attempting to take a drawing context, basically the entire area of a dialog box and save it as a bitmap so that I can open it up in Paint. I understand that I might be able to take a screen shot using Print Screen, but I only want the drawing area, not the menu and borders etc... This is my code, as far as I can tell it works until I make the call to GetDIBits. This function returns 0, and when I call GetLastError() it returns ERROR_FILE_NOT_FOUND. The function GetDIBits remarks that I must pass an bitmap that isn't selected into a DC. Could somebody give me a hint about where I am going wrong? I am not sure how to unselect an object from a DC, or how to create a copy of the DC after I Blt in the viewing area.
GetClientRect(hDlg, &rc); // Get the rectangle dimensions of the Dialog Box hdc = GetDC(hDlg); // Get a handle to the Drawing Context of the Dialog Box blankDC = GetDC(NULL); rc.bottom -= (GetSystemMetrics(SM_CYHSCROLL)+GetSystemMetrics(SM_CYEDGE)); hSavedXYContext = CreateCompatibleDC(hdc); // Create a drawing context that is // compatiable to the drawing context on the screen hSavedXYBitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left,rc.bottom-rc.top); hUnSelectedBitmap= CreateCompatibleBitmap(hdc,rc.right-rc.left,rc.bottom-rc.top); SelectObject(hSavedXYContext, hSavedXYBitmap); BitBlt(hSavedXYContext,0,0,rc.right-rc.left, rc.bottom-rc.top,hdc,0,0,SRCCOPY); // As far as I can tell above code does exactly what it should. It grabs a // handle to the drawing context, along with the rectangular coordinates of the // drawing context. Then after creating a compatible bitmap, and selecting it // into the saved drawing context, before blting it in. // Now we have the DC saved to hSavedContext, so we need to take it and create a // bitmap file that we can open in Paint. // This involves creating a bmp structure then writing it to a file. GetObject(hSavedXYBitmap,sizeof(BITMAP),(LPSTR)(&bmp)); cClrBits = (WORD)(bmp.bmPlanes*bmp.bmBitsPixel); cClrBits = 32; pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->
-
Guys, I am attempting to take a drawing context, basically the entire area of a dialog box and save it as a bitmap so that I can open it up in Paint. I understand that I might be able to take a screen shot using Print Screen, but I only want the drawing area, not the menu and borders etc... This is my code, as far as I can tell it works until I make the call to GetDIBits. This function returns 0, and when I call GetLastError() it returns ERROR_FILE_NOT_FOUND. The function GetDIBits remarks that I must pass an bitmap that isn't selected into a DC. Could somebody give me a hint about where I am going wrong? I am not sure how to unselect an object from a DC, or how to create a copy of the DC after I Blt in the viewing area.
GetClientRect(hDlg, &rc); // Get the rectangle dimensions of the Dialog Box hdc = GetDC(hDlg); // Get a handle to the Drawing Context of the Dialog Box blankDC = GetDC(NULL); rc.bottom -= (GetSystemMetrics(SM_CYHSCROLL)+GetSystemMetrics(SM_CYEDGE)); hSavedXYContext = CreateCompatibleDC(hdc); // Create a drawing context that is // compatiable to the drawing context on the screen hSavedXYBitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left,rc.bottom-rc.top); hUnSelectedBitmap= CreateCompatibleBitmap(hdc,rc.right-rc.left,rc.bottom-rc.top); SelectObject(hSavedXYContext, hSavedXYBitmap); BitBlt(hSavedXYContext,0,0,rc.right-rc.left, rc.bottom-rc.top,hdc,0,0,SRCCOPY); // As far as I can tell above code does exactly what it should. It grabs a // handle to the drawing context, along with the rectangular coordinates of the // drawing context. Then after creating a compatible bitmap, and selecting it // into the saved drawing context, before blting it in. // Now we have the DC saved to hSavedContext, so we need to take it and create a // bitmap file that we can open in Paint. // This involves creating a bmp structure then writing it to a file. GetObject(hSavedXYBitmap,sizeof(BITMAP),(LPSTR)(&bmp)); cClrBits = (WORD)(bmp.bmPlanes*bmp.bmBitsPixel); cClrBits = 32; pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->
Before you make a call to GetDIBBits call SelectObject again to release your selected bitmap.
-
Guys, I am attempting to take a drawing context, basically the entire area of a dialog box and save it as a bitmap so that I can open it up in Paint. I understand that I might be able to take a screen shot using Print Screen, but I only want the drawing area, not the menu and borders etc... This is my code, as far as I can tell it works until I make the call to GetDIBits. This function returns 0, and when I call GetLastError() it returns ERROR_FILE_NOT_FOUND. The function GetDIBits remarks that I must pass an bitmap that isn't selected into a DC. Could somebody give me a hint about where I am going wrong? I am not sure how to unselect an object from a DC, or how to create a copy of the DC after I Blt in the viewing area.
GetClientRect(hDlg, &rc); // Get the rectangle dimensions of the Dialog Box hdc = GetDC(hDlg); // Get a handle to the Drawing Context of the Dialog Box blankDC = GetDC(NULL); rc.bottom -= (GetSystemMetrics(SM_CYHSCROLL)+GetSystemMetrics(SM_CYEDGE)); hSavedXYContext = CreateCompatibleDC(hdc); // Create a drawing context that is // compatiable to the drawing context on the screen hSavedXYBitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left,rc.bottom-rc.top); hUnSelectedBitmap= CreateCompatibleBitmap(hdc,rc.right-rc.left,rc.bottom-rc.top); SelectObject(hSavedXYContext, hSavedXYBitmap); BitBlt(hSavedXYContext,0,0,rc.right-rc.left, rc.bottom-rc.top,hdc,0,0,SRCCOPY); // As far as I can tell above code does exactly what it should. It grabs a // handle to the drawing context, along with the rectangular coordinates of the // drawing context. Then after creating a compatible bitmap, and selecting it // into the saved drawing context, before blting it in. // Now we have the DC saved to hSavedContext, so we need to take it and create a // bitmap file that we can open in Paint. // This involves creating a bmp structure then writing it to a file. GetObject(hSavedXYBitmap,sizeof(BITMAP),(LPSTR)(&bmp)); cClrBits = (WORD)(bmp.bmPlanes*bmp.bmBitsPixel); cClrBits = 32; pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->
TheDelChop wrote:
I am not sure how to unselect an object from a DC
like this: HBITMAP hTheDCsOriginalBitmap = SelectObject(hSavedXYContext, hSavedXYBitmap); ... all the other stuff // when you're done: SelectObject(hSavedXYContext, hTheDCsOriginalBitmap);
image processing toolkits | batch image processing | blogging
-
Guys, I am attempting to take a drawing context, basically the entire area of a dialog box and save it as a bitmap so that I can open it up in Paint. I understand that I might be able to take a screen shot using Print Screen, but I only want the drawing area, not the menu and borders etc... This is my code, as far as I can tell it works until I make the call to GetDIBits. This function returns 0, and when I call GetLastError() it returns ERROR_FILE_NOT_FOUND. The function GetDIBits remarks that I must pass an bitmap that isn't selected into a DC. Could somebody give me a hint about where I am going wrong? I am not sure how to unselect an object from a DC, or how to create a copy of the DC after I Blt in the viewing area.
GetClientRect(hDlg, &rc); // Get the rectangle dimensions of the Dialog Box hdc = GetDC(hDlg); // Get a handle to the Drawing Context of the Dialog Box blankDC = GetDC(NULL); rc.bottom -= (GetSystemMetrics(SM_CYHSCROLL)+GetSystemMetrics(SM_CYEDGE)); hSavedXYContext = CreateCompatibleDC(hdc); // Create a drawing context that is // compatiable to the drawing context on the screen hSavedXYBitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left,rc.bottom-rc.top); hUnSelectedBitmap= CreateCompatibleBitmap(hdc,rc.right-rc.left,rc.bottom-rc.top); SelectObject(hSavedXYContext, hSavedXYBitmap); BitBlt(hSavedXYContext,0,0,rc.right-rc.left, rc.bottom-rc.top,hdc,0,0,SRCCOPY); // As far as I can tell above code does exactly what it should. It grabs a // handle to the drawing context, along with the rectangular coordinates of the // drawing context. Then after creating a compatible bitmap, and selecting it // into the saved drawing context, before blting it in. // Now we have the DC saved to hSavedContext, so we need to take it and create a // bitmap file that we can open in Paint. // This involves creating a bmp structure then writing it to a file. GetObject(hSavedXYBitmap,sizeof(BITMAP),(LPSTR)(&bmp)); cClrBits = (WORD)(bmp.bmPlanes*bmp.bmBitsPixel); cClrBits = 32; pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->
Another way to deselect a GDI object is to first save your DC context with CDC::SaveContextDC then restore it with CDC::RestoreContextDC when you finish with the objects you selected.
Where do you expect us to go when the bombs fall?