HRESULT ShowPic(CDC *pDC,CString lpstrFile,int x,int y, int nScrWidth, int nScrHeight)//对指定路径文件缩放处理
{
IPicture *pPic;
IStream *pStm;
BOOL bResult;
HANDLE hFile=NULL;
DWORD dwFileSize,dwByteRead;
//打开硬盘中的图形文件
hFile=CreateFile(lpstrFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile!=INVALID_HANDLE_VALUE)
{
dwFileSize=GetFileSize(hFile,NULL);//获取文件字节数
if(dwFileSize==0xFFFFFFFF)
{
CloseHandle(hFile);
return E_FAIL;
}
}
else
{
return E_FAIL;
}
//分配全局存储空间
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
if(hGlobal==NULL)
return E_FAIL;
LPVOID pvData =GlobalLock(hGlobal);
if(pvData==NULL)//锁定分配内存块
{
GlobalFree(hGlobal);
return E_FAIL;
}
ReadFile(hFile,pvData,dwFileSize,&dwByteRead,NULL);//把文件读入内存缓冲区
CloseHandle(hFile);//关闭打开的文件
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
//装入图形文件
bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);
GlobalFree(hGlobal);
if(FAILED(bResult))
return E_FAIL;
long hmWidth;//图片的真实宽度
long hmHeight;//图片的真实高度
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//将图形输出到屏幕上(有点像BitBlt)
bResult=pPic->Render(pDC->m_hDC,x,y,nScrWidth,nScrHeight,0,hmHeight,hmWidth,-hmHeight,NULL);
pPic->Release();
if(SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}
总结知识点:
1.MFC显示图片 (Qt是创建了一个QPixmap对象进行的画QPixmap,来进行实现的)
//****************显示BMP格式图片****************//
void CDrawMapTest8_23View::ShowBitmap(CDC *pDC, CString BmpName,CRect rect)
{
CImage img;
HRESULT ret=img.Load(BmpName);
HBITMAP m_hBitmap=img.Detach();
if( m_bitmap.m_hObject )
{
m_bitmap.Detach(); //切断CWnd和窗口联系
}
m_bitmap.Attach(m_hBitmap); //将句柄HBITMAP m_hBitmap与CBitmap m_bitmap关联
/*
//边界
CRect rect;
GetClientRect(&rect);
//图片显示(x,y)起始坐标
int m_showX=0;
int m_showY=0;
int m_nWindowWidth = rect.right - rect.left; //计算客户区宽度
int m_nWindowHeight = rect.bottom - rect.top; //计算客户区高度
*/
//定义并创建一个内存设备环境DC
CDC dcBmp;
if( !dcBmp.CreateCompatibleDC(pDC) ) //创建兼容性的DC
return;
BITMAP m_bmp; //临时bmp图片变量
m_bitmap.GetBitmap(&m_bmp); //将图片载入位图中
CBitmap *pbmpOld = NULL;
dcBmp.SelectObject(&m_bitmap); //将位图选入临时内存设备环境
//图片显示调用函数stretchBlt
pDC->StretchBlt(0,0,m_bmp.bmWidth,m_bmp.bmHeight,&dcBmp,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY);
dcBmp.SelectObject(pbmpOld); //恢复临时DC的位图
DeleteObject(&m_bitmap); //删除内存中的位图
dcBmp.DeleteDC(); //删除CreateCompatibleDC得到的图片DC
}
{
IPicture *pPic;
IStream *pStm;
BOOL bResult;
HANDLE hFile=NULL;
DWORD dwFileSize,dwByteRead;
//打开硬盘中的图形文件
hFile=CreateFile(lpstrFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile!=INVALID_HANDLE_VALUE)
{
dwFileSize=GetFileSize(hFile,NULL);//获取文件字节数
if(dwFileSize==0xFFFFFFFF)
{
CloseHandle(hFile);
return E_FAIL;
}
}
else
{
return E_FAIL;
}
//分配全局存储空间
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
if(hGlobal==NULL)
return E_FAIL;
LPVOID pvData =GlobalLock(hGlobal);
if(pvData==NULL)//锁定分配内存块
{
GlobalFree(hGlobal);
return E_FAIL;
}
ReadFile(hFile,pvData,dwFileSize,&dwByteRead,NULL);//把文件读入内存缓冲区
CloseHandle(hFile);//关闭打开的文件
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
//装入图形文件
bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);
GlobalFree(hGlobal);
if(FAILED(bResult))
return E_FAIL;
long hmWidth;//图片的真实宽度
long hmHeight;//图片的真实高度
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//将图形输出到屏幕上(有点像BitBlt)
bResult=pPic->Render(pDC->m_hDC,x,y,nScrWidth,nScrHeight,0,hmHeight,hmWidth,-hmHeight,NULL);
pPic->Release();
if(SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}
总结知识点:
1.MFC显示图片 (Qt是创建了一个QPixmap对象进行的画QPixmap,来进行实现的)
//****************显示BMP格式图片****************//
void CDrawMapTest8_23View::ShowBitmap(CDC *pDC, CString BmpName,CRect rect)
{
CImage img;
HRESULT ret=img.Load(BmpName);
HBITMAP m_hBitmap=img.Detach();
if( m_bitmap.m_hObject )
{
m_bitmap.Detach(); //切断CWnd和窗口联系
}
m_bitmap.Attach(m_hBitmap); //将句柄HBITMAP m_hBitmap与CBitmap m_bitmap关联
/*
//边界
CRect rect;
GetClientRect(&rect);
//图片显示(x,y)起始坐标
int m_showX=0;
int m_showY=0;
int m_nWindowWidth = rect.right - rect.left; //计算客户区宽度
int m_nWindowHeight = rect.bottom - rect.top; //计算客户区高度
*/
//定义并创建一个内存设备环境DC
CDC dcBmp;
if( !dcBmp.CreateCompatibleDC(pDC) ) //创建兼容性的DC
return;
BITMAP m_bmp; //临时bmp图片变量
m_bitmap.GetBitmap(&m_bmp); //将图片载入位图中
CBitmap *pbmpOld = NULL;
dcBmp.SelectObject(&m_bitmap); //将位图选入临时内存设备环境
//图片显示调用函数stretchBlt
pDC->StretchBlt(0,0,m_bmp.bmWidth,m_bmp.bmHeight,&dcBmp,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY);
dcBmp.SelectObject(pbmpOld); //恢复临时DC的位图
DeleteObject(&m_bitmap); //删除内存中的位图
dcBmp.DeleteDC(); //删除CreateCompatibleDC得到的图片DC
}