Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/articles/mfc-tutorials/)
-   -   Display Bitmap in a Picture Box using MFC from a file (http://www.go4expert.com/articles/display-bitmap-picture-box-using-mfc-t350/)

shabbir 9Jun2005 13:05

Display Bitmap in a Picture Box using MFC from a file
 
1 Attachment(s)
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.

picPreview.SetBitmap(CString m_sBitmap)
PictureBox.h
Code: CPP

#pragma once
   
   // CPictureBox
   
   class CPictureBox : public CStatic
   {
    DECLARE_DYNAMIC(CPictureBox)
   
   public:
    CPictureBox();
    virtual ~CPictureBox();
    void SetBitmap(CString strBitmap);
   
   protected:
    DECLARE_MESSAGE_MAP()
    void ShowBitmap(CPaintDC *pDC);
   
    CString m_sBitmap;
    CBitmap m_bmpBitmap;
    BITMAP bm;
   public:
    afx_msg void OnPaint();
   };

PictureBox.cpp
Code: CPP

// PictureBox.cpp : implementation file
   //
   
   #include "stdafx.h"
   #include "PictureBox.h"
   #include ".\picturebox.h"
   
   
   // CPictureBox
   
   IMPLEMENT_DYNAMIC(CPictureBox, CStatic)
   CPictureBox::CPictureBox()
   {
   }
   
   CPictureBox::~CPictureBox()
   {
   }
   
   
   BEGIN_MESSAGE_MAP(CPictureBox, CStatic)
    ON_WM_PAINT()
   END_MESSAGE_MAP()
   
   
   
   // CPictureBox message handlers
   
   void CPictureBox::ShowBitmap(CPaintDC *pdc)
   {
    //Create a device context to load the bitmap into
    CDC dcMem;
    dcMem.CreateCompatibleDC(pdc);
   
    //Get the Display area available
    CRect lRect;
    GetClientRect(lRect);
    lRect.NormalizeRect();
   
    //select the bitmap into compatible device context
    CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&m_bmpBitmap);
    //m_bmpBitmap.SetBitmapDimension(lRect.Width(),lRect.Height());
   
    //copy & resize the window to the dialog window
    pdc->StretchBlt(0,0,lRect.Width(),lRect.Height(),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
   }
   void CPictureBox::OnPaint()
   {
    CPaintDC dc(this); // device context for painting
   
    RECT rect;
    GetClientRect(&rect);
    dc.FillSolidRect(&rect, RGB(255,255,255));
   
    if(m_sBitmap!="")
     ShowBitmap(&dc);
   }
   void CPictureBox::SetBitmap(CString strBitmap)
   {
    m_sBitmap = strBitmap;
   
    HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
    m_sBitmap, IMAGE_BITMAP, 0, 0,
    LR_LOADFROMFILE | LR_CREATEDIBSECTION);
   
    // Do we have a valid handle for the loaded image?
    if (hBitmap)
    {
     // Delete the current bitmap
     if (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.

Naresh 21Jun2006 12:48

Re: Display Bitmap in a Picture Box using MFC from a file
 
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.

shabbir 21Jun2006 15:38

Re: Display Bitmap in a Picture Box using MFC from a file
 
Try studying the code snippets above and it has GetBitmap function. If you do a GetBitmap on a NULL bitmap there is a possibility that you get an exception.

gabedude 26Sep2006 01:13

Re: Display Bitmap in a Picture Box using MFC from a file
 
Shabbir,

This was helpfull, but in VC 8.0 (VS2005), you have to do things a little different.

1. The IDE will not let you add a variable of CStatic type. You have to edit the code directly:

Calling code header:

CPictureBox m_PicBox;

Calling code implementaion:

void XXX::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
...
DDX_Control(pDX, IDC_PICTBOX1, m_PicBox);
}


2. You have to overload the CStatic DrawItem function because winctrl1.cpp has this in it:

// Derived class is responsible for implementing all of these handlers
// for owner/self draw controls
void CStatic::DrawItem(LPDRAWITEMSTRUCT)
{
ASSERT(FALSE);
}


PictureBox.h:

void DrawItem(LPDRAWITEMSTRUCT ds);

PictureBox.cpp:

void CPictureBox::DrawItem(LPDRAWITEMSTRUCT ds)
{
//Do nothing
}


-Gabe

shabbir 26Sep2006 10:20

Re: Display Bitmap in a Picture Box using MFC from a file
 
Gabe,

First of all Welcome to G4EF.

It allows to add the variable of type CStatic but the name of the control should not be IDC_STATIC. Let me know if I am missing anything.

-Shabbir

gabedude 26Sep2006 18:02

Re: Display Bitmap in a Picture Box using MFC from a file
 
Shabbir,

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.

Thanks again for the code.

-Gabe

shabbir 26Sep2006 18:58

Re: Display Bitmap in a Picture Box using MFC from a file
 
Yes that I havent tested might be a good tip.

Cuckoo 23Dec2006 19:39

Re: Display Bitmap in a Picture Box using MFC from a file
 
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)?

Cuckoo 23Dec2006 19:44

Re: Display Bitmap in a Picture Box using MFC from a file
 
i am a second year software engineering student, so i have little experience in visual c++ programming but some experience in c++... help! :(

shabbir 24Dec2006 00:04

Re: Display Bitmap in a Picture Box using MFC from a file
 
Quote:

Originally Posted by Cuckoo
if you could give me a little explanation on how to use the picturebox?

Thats what the article is all about.
Quote:

Originally Posted by Cuckoo
my second question is how can i do a frame window which shows a slide show (say using that picture box)?

Changing the images is what you meant by slideshow then use the above code snippets for changing the images in the timer event.


All times are GMT +5.5. The time now is 23:32.