您的位置:首页 > 其它

MFC 加载jpg图片

2014-05-19 17:05 169 查看
转载http://blog.sina.com.cn/s/blog_4ac0eb8101010ico.html

IPicture *m_picture;
OLE_XSIZE_HIMETRIC m_width;
OLE_YSIZE_HIMETRIC m_height;
BOOL m_IsShow;
2. 在Dlg.cpp的OnPaint函数中加入:

CPaintDC dc(this);
CFile m_file("D:\\1.jpg",CFile::modeRead );
//获取文件长度
DWORD m_filelen = m_file.GetLength();
//在堆上分配空间
HGLOBAL m_hglobal = GlobalAlloc(GMEM_MOVEABLE,m_filelen);

LPVOID pvdata = NULL;
//锁定堆空间,获取指向堆空间的指针
pvdata = GlobalLock(m_hglobal);

//将文件数据读区到堆中
m_file.ReadHuge(pvdata,m_filelen);

IStream* m_stream;

GlobalUnlock(m_hglobal);

//在堆中创建流对象
CreateStreamOnHGlobal(m_hglobal,TRUE,&m_stream);

//利用流加载图像
OleLoadPicture(m_stream,m_filelen,TRUE,IID_IPicture,(LPVOID*)&m_picture);
m_stream->Release();

m_picture->get_Width(&m_width);// 宽高,MM_HIMETRIC 模式,单位是0.01毫米
m_picture->get_Height(&m_height);
m_IsShow = TRUE;
m_file.Close();

if (m_IsShow==TRUE){
CRect rect;
GetClientRect(rect);
int nW, nH;
nW = (int)(rect.Width());
nH = (int)(rect.Height());
m_picture->Render(dc,0,0,nW,nH,0,m_height,m_width,-m_height,&rect);
}
CDialog::OnPaint();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: