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".
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.Code: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 - picRect.top),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.
I hope that this tutorial was useful and that it helps those of you who needed it. Good Luck.