title: C++ WinApi Draw image .jpg to new window?
body: <p>I am new to the Windows API. Using some of the Windows sample code supplied on the Winapi tutorials:</p>
  
  <p><a href="https://msdn.microsoft.com/enus/library/windows/desktop/ms536051.aspx" rel="nofollow">Graphics.DrawImage(Image*, const Rect) method</a></p>
  
  <p>I'm looking to open a <code>.jpg</code> image and draw it to a new window I have created.  The trouble is that I'm not sure exactly how to use the <code>VOID Example_DrawImage9(HDC hdc)</code> method with my existing window. My first instinct was to invoke it inside <code>case WM_PAINT</code> in the callback procedure and use the <code>hdc</code> from there,  but the image does not display. How do I know the correct <code>hdc</code> to supply? And where should I invoke the method?</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include "stdafx.h" 
  #include &lt;objidl.h&gt;
  #include &lt;gdiplus.h&gt;
  
  using namespace Gdiplus;
  #pragma comment (lib,"Gdiplus.lib")
  
  //*************************************************** added for gdiplus
  HWND hEdit;
  
  //************************************************how do I use this method   with the window I have created below?
  VOID Example_DrawImage9(HDC hdc){
      Graphics graphics(hdc); // Create an Image object.
      Image image(L"C:/Users/Me/Desktop/fuzz.jpg"); // Create a Pen object.
      Pen pen(Color(255, 255, 0, 0), 2); // Draw the original source image.
      graphics.DrawImage(&amp;image, 10, 10); // Create a Rect object that specifies the destination of the image.
      Rect destRect(200, 50, 150, 75); // Draw the rectangle that bounds the image.
      graphics.DrawRectangle(&amp;pen, destRect); // Draw the image.
      graphics.DrawImage(&amp;image, destRect);
  }
  //*********************************************************************************************
  
  
  
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  
  int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  {
      const wchar_t CLASS_NAME[] = L"Sample Window Class";
      WNDCLASS wc = {}; 
      wc.lpfnWndProc = WindowProc; //attach this callback procedure
      wc.hInstance = hInstance; //handle to application instance
      wc.lpszClassName = CLASS_NAME; 
      RegisterClass(&amp;wc); //register wc
      // Create the window.
      HWND hwnd = CreateWindowEx( 
          0,                              // Optional window styles.
          CLASS_NAME,                     // Window class
          L"Learn to Program Windows",    // Window text
          WS_OVERLAPPEDWINDOW,            // Window style
  
                                          // Size and position
          CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  
          NULL,       // Parent window    
          NULL,       // Menu
          hInstance,  // Instance handle
          NULL        // Additional application data
          );
  
      if (hwnd == NULL){
          return 0;
      }
  
      ShowWindow(hwnd, nCmdShow);
  
      MSG msg = {};
      while (GetMessage(&amp;msg, NULL, 0, 0)){
      TranslateMessage(&amp;msg);
      DispatchMessage(&amp;msg);
  }
  return 0;
  }
  
  //callback procedure for this window, takes in all the window details
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
      switch (uMsg){
          case WM_DESTROY: 
              PostQuitMessage(0);
              return 0;
  
          case WM_PAINT:{
              PAINTSTRUCT ps;
              HDC hdc = BeginPaint(hwnd, &amp;ps);
              //***************************************************************
              //do we call DrawImage here? what do we need to pass as hdc?
              //Example_DrawImage9(HDC hdc);//?????????????
              //***************************************************************
              FillRect(hdc, &amp;ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
              EndPaint(hwnd, &amp;ps);
          }
          return 0;
      }
      return DefWindowProc(hwnd, uMsg, wParam, lParam);
  }
  </code></pre>