title: Drawing a jpg in MFC
body: <p>I've been trying to show a jpg image in MFC, but I can't get it drawn. I'm a complete MFC newbie an everything I got up until now is mostly adapted from things I found on the net. Currently I have this:</p>
  
  <p>Picture.h:</p>
  
  <pre><code>#pragma once
  #include &lt;afxwin.h&gt;
  
  class Picture
  {
  public:
     Picture();
  
     bool load(LPCTSTR filePath);
  
     bool draw( CDC* deviceContext
              , CRect clientRect
              , LPCRECT prcMFBounds);
  
     CSize getSize(CDC* pDC);
  
  private:
     LPPICTURE m_picture;
  };
  </code></pre>
  
  <p>Picture.cpp:</p>
  
  <pre><code>#include "Picture.h"
  
  Picture::Picture()
     : m_picture(0)
  {
  }
  
  bool Picture::load(LPCTSTR szFile)
  {
     HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
     if (hFile == INVALID_HANDLE_VALUE)
        return false;
  
     DWORD dwFileSize = GetFileSize(hFile, NULL);
     if (dwFileSize == (DWORD)-1)
     {
        CloseHandle(hFile);
        return false;
     }
  
     LPVOID pvData = NULL;
  
     // alloc memory based on file size
     HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
     if (hGlobal == NULL)
     {
        CloseHandle(hFile);
        return false;
     }
  
     pvData = GlobalLock(hGlobal);
  
     if (pvData == NULL)
     {
        GlobalUnlock(hGlobal);
        CloseHandle(hFile);
        return false;
     }
  
     DWORD dwBytesRead = 0;
  
     // read file and store in global memory
     bool bRead = ReadFile(hFile, pvData, dwFileSize, &amp;dwBytesRead, NULL) != 0;
  
     GlobalUnlock(hGlobal);
     CloseHandle(hFile);
  
     if (!bRead)
        return false;
  
     LPSTREAM pstm = NULL;
  
     // create IStream* from global memory
     HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &amp;pstm);
     if (!(SUCCEEDED(hr)))
     {
        if (pstm != NULL)
           pstm-&gt;Release();
        return false;
     }
  
     else if (pstm == NULL)
        return false;
  
     // Create IPicture from image file
     if (m_picture)
        m_picture-&gt;Release();
  
     hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&amp;(m_picture));
     if (!(SUCCEEDED(hr)))
     {
        pstm-&gt;Release();
        return false;
     }
  
     else if (m_picture == NULL)
     {
        pstm-&gt;Release();
        return false;
     }
     pstm-&gt;Release();
  
     return true;
  }
  
  bool Picture::draw(CDC* deviceContext, CRect clientRect, LPCRECT prcMFBounds)
  {
     if (clientRect.IsRectNull())
     {
        CSize imageSize = getSize(deviceContext);
        clientRect.right = imageSize.cx;
        clientRect.bottom = imageSize.cy;
     }
  
     long pictureWidth = 0;
     long pictureHeigth = 0;
  
     m_picture-&gt;get_Width(&amp;pictureWidth);
     m_picture-&gt;get_Height(&amp;pictureHeigth);
  
     m_picture-&gt;Render( *deviceContext
                      , clientRect.left
                      , clientRect.top
                      , clientRect.Width()
                      , clientRect.Height()
                      , 0
                      , pictureHeigth
                      , pictureWidth
                      , -pictureHeigth
                      , prcMFBounds);
     return true;
  }
  
  CSize Picture::getSize(CDC* deviceContext)
  {
     if (!m_picture)
        return CSize(0,0);
  
     LONG width, height; // HIMETRIC units
     m_picture-&gt;get_Width(&amp;width);
     m_picture-&gt;get_Height(&amp;height);
     CSize size(width, height);
     if (deviceContext==NULL)
     {
        CWindowDC dc(NULL);
        dc.HIMETRICtoDP(&amp;size); // convert to pixels
     }
     else
     {
        deviceContext-&gt;HIMETRICtoDP(&amp;size);
     }
     return size;
  }
  </code></pre>
  
  <p>PictureView.h:</p>
  
  <pre><code>#pragma once
  
  #include &lt;afxwin.h&gt;
  #include &lt;string&gt;
  
  class Picture;
  
  class PictureView : public CStatic
  {
  public:
     PictureView(std::string path);
  
  protected:
     virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  
  private:
     Picture* m_picture;
  };
  </code></pre>
  
  <p>PictureView.cpp:</p>
  
  <pre><code>#include "PictureView.h"
  #include "Picture.h"
  
  PictureView::PictureView(std::string path)
  {
     m_picture = new Picture();
     m_picture-&gt;load(path.c_str());
  }
  
  void PictureView::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
  {
     CRect rect;
     GetClientRect(&amp;rect);
     m_picture-&gt;draw(GetDC(), rect, rect);
  }
  </code></pre>
  
  <p>Constructor call:</p>
  
  <pre><code>CWnd* GenericChildWidget::addImage(std::string path, int horizontalPos, int width, int verticalPos, int height, int childId)
  {
     PictureView* image = new PictureView(path);
     image-&gt;Create("", SS_OWNERDRAW, CRect(horizontalPos, verticalPos, width + horizontalPos, height + verticalPos), this, childId);
     return image;
  }
  </code></pre>
  
  <p>The problem is that I can't get the DrawItem function to be called. What am I doing wrong?
  Any help will be appreciated.</p>