Display Bitmap in a Picture Box using MFC from a file

This is an article on Display Bitmap in a Picture Box using MFC from a file in MFC.

Rated 5.00 By 1 users

When you insert a picture on a Dialog box the main aim of your dialog box is just not to display the Resource bitmaps that are added at compile time but to display the image from a file. Now to display a bitmap I could not find many good resources but if you google out with the keyword LoadPic.exe you can find some help with that sample but that is way too complicated for a simple image display and so I thought of all doing it with my shallow knowledge of Subclasing.

Steps
1. Add a picture Box to your application
2. Change the Type of the picture box to OWNER_DRAW. [Skip this step. Its not needed as corrected by lplover2k]
3. Add a variable to the picture box. By default it will be CStatic. I call it picPreview
4. Change the CStatic to CPictureBox. Don't forget to add the header files at the top.
5. Call the SetBitmap function to display the URL in the picture box.

// Do we have a valid handle for the loaded image?if(hBitmap){// Delete the current bitmapif(m_bmpBitmap.DeleteObject()) m_bmpBitmap.Detach(); // If there was a bitmap, detach it// Attach the currently loaded bitmap to the bitmap object m_bmpBitmap.Attach(hBitmap);} m_bmpBitmap.GetBitmap(&bm); //Get Bitmap Structure Invalidate();}

The attached zip file contains the above 2 header files

Edited the code to correct some errors regarding unnecessary include files. Thanks to lplover2k.

Related Articles

Hi sabbir iam naresh from hyderabad.iam getting an error in bitmap loading and displaying it. after attaching new bitmap i had called GetBitmap() function and iam getting an error overhere .plz solve my error ,or what is error.

You may be right. I might have changed the variable name after I added the code to the source file. However, you still must overload DrawItem() in the CPictureBox class. I was getting an assert when I pressed the CTRL key in my program until I did this.

hello, i am new to visual c++ and i was wondering if you could give me a little explanation on how to use the picturebox? my second question is how can i do a frame window which shows a slide show (say using that picture box)?