Thread: How-To: Load Pictures in MFC. Code Inside.

  1. #1
    Registered User Xei's Avatar
    Join Date
    May 2002

    How-To: Load Pictures in MFC. Code Inside.

    I have seen many posts regarding to how a person can Load Pictures in Visual C++. So, like them, I was curious and have found out how. Now I shall show you how I have accomplished this in MFC. It is quite simple, and I will explain it to the best of my knowlege.

    First off, here are the steps you must consider before just copying and pasting code.


    First: You must create a Device Context(DC). A Device Context is placed in Memory from the API function: "CreateCompatibleDC(HDC)", The DC that you will create will hold your Bitmap Image.

    Second: You must load your Bitmap File by using the API function: "LoadImage(...)", the return value of this function will be a Handle to the Bitmap which will now be loaded into memory.

    Third: After you have created your Device Context(DC) and you have loaded your Image into memory, you must somehow place the newly Loaded image into the DC. This can be done by using the API function "SelectObject(HDC, HGDIOBJ)".

    Fourth: Now we have a completely usable DC(usually notated as 'hDC'). Now we want to write it to something... but how? To write an image from one DC to the DC of a PictureBox we will need to find out some information from the PictureBox on our form. The information we must record from our PictureBox will be its height and its width. I have bound a Control Variable to my PictureBox, it's name is: "m_Picture". We can record the PictureBox's Rectangular values from this function: "m_Picture.GetWindowRect(LPRECT)".

    Fifth: We will use the API function "BitBlt(...)" to write from our DC to our PictureBox's DC. But, we have not yet found our PictureBox's DC. To find the PictureBox's DC we can use the function: "::GetDC(m_Picture.m_hWnd)". Now we have our PictureBox's Rectangular values, our PictureBox's DC, and we have loaded our Bitmap into a temporary DC of our own.

    Sixth: After you have used the BitBlt function to write your temporary DC to the PictureBox's DC you may decide that it is a good idea to delete the temporary Device Context which is holding the Bitmap. You may use the "DeleteDC(HDC)" API function to delete the DC when you are finished with it.

    Here is the commented source code that I have used in MFC(Microsoft Foundation Class) to load Bitmaps into a PictureBox. Please keep in mind that I have bounded a Control Variable to my PictureBox, its name is "m_Picture".

    HANDLE hBitmap;  //Holds the Handle of the Bitmap once it is loaded.
    HDC hDC;  //Our temporary Device Context, which will be used to hold our Bitmap later on.
    RECT picRect; //Used to gather the Rectangular values of our PictureBox.
    HDC picHDC;  //Holds the value of our PictureBox's Device Context.
    hDC = CreateCompatibleDC(NULL);  //Create's a compatible Device Context(DC) and stores its value in 'hDC'.
    hBitmap = LoadImage(0,"C:\\Documents and Settings\\Xei\\Desktop\\Exclusion.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //This loads our Image into memory and stores the handle in the variable "hBitmap". You may change the Directory from this API call to one containing your Image.
    SelectObject(hDC,hBitmap); //Puts our loaded Image into our temporary Device Context.
    picHDC = ::GetDC(m_Picture.m_hWnd); //Obtains the Device Context for our PictureBox. So that we may later write to it.
    m_Picture.GetWindowRect(&picRect); //Obtains our Rectangular values for our PictureBox.
    BitBlt(picHDC,0,0,(picRect.right - picRect.left),(picRect.bottom -,hDC,0,0,SRCCOPY); //Copys(BitBlits) our image into the PictureBox. It copies our temporary DC to our PictureBox's DC.
    DeleteDC(hDC); //Deletes our temporary Device Context that we created earlier. We do this so that we can conserve memory and if you do not delete your device context it  may render it un-usable at a later time.
    When you are finished using the PictureBox's Device Context and you wish to free it, then do not use DeleteDC. You should never use DeleteDC to delete a Device Context which was obtained by using the GetDC API call. Instead you should use ReleaseDC to release the device.

    I hope that this tutorial was useful and that it helps those of you who needed it. Good Luck.

  2. #2
    Registered User Xei's Avatar
    Join Date
    May 2002

    One More Thing

    I forgot to mention:
    You will need to include windows.h for the API calls.
    If your PictureBox is not displaying the image then make sure that the file you are loading is a valid Bitmap Image. You may also try Redrawing the PictureBox like so:


    When you move your Form the PictureBox will clear, you will have to Redraw / BitBlt the Picture back onto the PictureBox each time the Form is Re-Painted. So you may want to place some code inside of your CDialog::OnPaint() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My ini load code.
    By RMDan in forum C Programming
    Replies: 16
    Last Post: 06-25-2009, 12:07 AM
  2. MFC MDI Load New View
    By jmw_misc in forum C++ Programming
    Replies: 0
    Last Post: 02-06-2009, 03:30 PM
  3. labels inside the code #pragma mark label
    By nacho4d in forum C++ Programming
    Replies: 6
    Last Post: 01-11-2009, 02:50 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM