显示在缓冲区图片

自绘一张图片的方法:

首先是声明部分:

#include "initguid.h"
#include "Imaging.h"

必须声明这两个头文件,而且顺序不能反,呵呵,这个地方我郁闷了一下,所以在此说明。

下面直接复制到代码,然后在sd卡里面添加相应的图片就行了;

void CTest_p_w_picpathDlg::OnPaint()
{
 
 CPaintDC dc(this); // device context for painting
 
 // TODO: Add your message handler code here
 
 // Do not call CDialog::OnPaint() for painting messages
 HRESULT hr ; //
返回结果
 IImagingFactory * pImagingFactory =NULL; //Image
工厂接口对象
 IImage * pImage = NULL; //Image
接口对象
 LPCTSTR szFileName = L"\\SDMDM\\pictures1.png"; //
图象文件

 //初始化COM环境
 
    if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
    {
        TRACE(L"COINIT_MULTITHREADED ERROR");
  return;
    }

 //得到Image工厂接口对象
  CoCreateInstance (CLSID_ImagingFactory,
  NULL,
  CLSCTX_INPROC_SERVER,
  IID_IImagingFactory,
  (void **)&pImagingFactory);


   if (FAILED(hr))
 {
  TRACE(L"IMAGE FACTORY CREATED ERROR");
  goto finish;
 }

 //加载图象文件到IImage接口对象中
 hr = pImagingFactory->CreateImageFromFile(szFileName,&pImage);

 //
 if (FAILED(hr))
 {
  TRACE(L"IMAGE LOAD ERROR");
  goto finish;
 } 
 {
 //
开始绘制图象
 //
得到设备环境
    CDC *pDC = GetDC();
 
 //
定义绘制尺寸
 RECT rect;
 rect.left = 110; 
 rect.top = 110; 
 rect.right  = 200; 
 rect.bottom = 80;

 GetClientRect(&rect);

 //绘制图片
 pImage->Draw(pDC->m_hDC, &rect, NULL);

 //释放设备环境
 ReleaseDC(pDC);
 }
finish:
 //
释放IImage接口对象
    if (pImage)
        pImage->Release();
       
 //
释放IImagingFactory接口对象
    if (pImagingFactory)
        pImagingFactory->Release();

 //释放程序占用的COM资源
    CoUninitialize();
 
 
}