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. Unselecting a bitmap in a context

Unselecting a bitmap in a context

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelptutorialquestion
4 Posts 4 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.
  • T Offline
    T Offline
    TheDelChop
    wrote on last edited by
    #1

    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->

    O C K 3 Replies Last reply
    0
    • T TheDelChop

      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->

      O Offline
      O Offline
      Optimus Chaos
      wrote on last edited by
      #2

      Before you make a call to GetDIBBits call SelectObject again to release your selected bitmap.

      1 Reply Last reply
      0
      • T TheDelChop

        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->

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • T TheDelChop

          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->

          K Offline
          K Offline
          KaRl
          wrote on last edited by
          #4

          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?

          Fold with us! ¤ flickr

          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