WinCE6.0学习笔记(图像读取并显示在MFC控件中)

我知道的方法有两种:利用IImage和利用BitBlt。平台mini2440
法一:IImage
加入头文件
#include <initguid.h>
#include <imaging.h>

#pragma comment (lib,"Ole32.lib")

注意顺序


/*我把下面代码放在一个按键响应函数里*/

CDC *pDC;
CRect rc;
GetDlgItem(IDC_EDIT1)->GetClientRect(rc);//IDC_EDIT1为显示图片的控件的ID
pDC = GetDlgItem(IDC_EDIT1)->GetDC();

IImage * m_pImage;
IImagingFactory * m_pImagingFactory;

HRESULT hr;

//COM初始化
if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
    goto END;
}
//创建COM实例
if(FAILED(hr = CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**) &m_pImagingFactory)))
{
    goto END;
}
//从文件中创建图片
if(FAILED(hr = m_pImagingFactory->CreateImageFromFile(_T("redhat.bmp"), &m_pImage)))//redhat.bmp是需要显示的图片
{
    goto END;
}
//绘制图片
if(FAILED(hr = m_pImage->Draw(pDC->m_hDC,&rc,NULL)))
{
    goto END;
}

END:
//释放资源
if(m_pImage != NULL)
{
    m_pImage->Release();
    m_pImage = NULL;
}

if(m_pImagingFactory != NULL)
{
    m_pImagingFactory->Release();
    m_pImagingFactory = NULL;
}

CoUninitialize();

法二:BitBlt

/*我把下面代码放在一个按键响应函数里*/

CDC *dc;
CDC memdc;
HBITMAP hBitmap;
HBITMAP hOldbmp;

hBitmap=SHLoadDIBitmap(_T("redhat.bmp"));//redhat.bmp是需要显示的图片
dc= GetDlgItem(IDC_EDIT1)->GetDC();//IDC_EDIT1为显示图片的控件的ID

if(hBitmap==NULL)
{
    RETAILMSG(1,(TEXT("打开错误!\r\n")));
}

memdc.CreateCompatibleDC(dc);
hOldbmp=(HBITMAP)memdc.SelectObject(hBitmap);
dc->BitBlt(0,0,200,200,&memdc,0,0,SRCCOPY);
RETAILMSG(1,(TEXT("bitblt done!\r\n")));

memdc.SelectObject(hOldbmp);
memdc.DeleteDC();




你可能感兴趣的:(server,null,mfc,平台,WinCE)