I recently blogged about one of the sessions I’m presenting at ESC East on application development, and showed the ~30 lines of managed code needed to pull an RSS feed from Frameit.live.com, parse the XML, and then pull and display images from the feed.

I got a couple of e-mails asking for the code needed to use the native code Win32 imaging APIs to load and display an image – here’s the code.

#define INITGUID
#include <initguid.h>
#include <imaging.h>

IImagingFactory *pImgFactory = NULL;
IImage *pImage = NULL;

// Initialize COM and Imaging API
HRESULT hr;
hr=CoInitializeEx(NULL, COINIT_MULTITHREADED);
hr=CoCreateInstance (CLSID_ImagingFactory,
                     NULL,
                     CLSCTX_INPROC_SERVER,
                     IID_IImagingFactory,
                     (void **)&pImgFactory);

// Load an image from the file system
hr=pImgFactory->CreateImageFromFile(strFileName, &pImage);

// Draw the image to fit the size of the client area
// This is from the WM_PAINT handler in wndproc
case WM_PAINT:
  RECT rcClient;
  hdc = BeginPaint(hWnd, &ps);
  // Get Client Area Dimensions
  GetClientRect(hWnd,&rcClient);

  int iWidth=rcClient.right;
  int iHeight=rcClient.bottom;
  IImage *pThumbImage = NULL;
  // Scale the image to fit the client area
  pImage->GetThumbnail(iWidth,iHeight,&pThumbImage);
  // Draw the thumbnail.
  pThumbImage->Draw(hdc,&rcClient,NULL);
  EndPaint(hWnd, &ps);
break;

Note that this isn’t the full program, just the snippets needed to get this to work.

Once ESC East is done I will post the rest of the code.

- Mike