给你个例子 利用流
工程基于对话框 名字是IPicture 建好工程后 加入下面的代码 就可以显示图片了
// IPictureDlg.h : 头文件
BOOL DisplayPic(CString strFileName, HWND hWnd, int nScrWidth, int nScrHeight);
// IPictureDlg.cpp : 实现文件
BOOL CIPictureDlg::DisplayPic(CString strFileName, HWND hWnd, int nScrWidth, int nScrHeight)
{
HDC hDC=::GetDC(hWnd);
IPicture *pPicture;
IStream *pStream;
HRESULT hResult;
HANDLE hFile = NULL;
DWORD dwFileSize,dwByteRead;
hFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return E_FAIL;
}
dwFileSize = GetFileSize(hFile, NULL);
HGLOBAL hGlobal = ::GlobalAlloc(GHND, dwFileSize);
if(hGlobal == NULL)
{
AfxMessageBox("图形文件装载出错!");
return E_FAIL;
}
LPVOID lpData = NULL;
lpData = ::GlobalLock(hGlobal);
ReadFile(hFile, lpData, dwFileSize, &dwByteRead, NULL);
::GlobalUnlock(hGlobal);
if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK)
{
AfxMessageBox("流初始化失败!");
return E_FAIL;
}
hResult = OleLoadPicture(pStream, dwFileSize, TRUE, IID_IPicture, (LPVOID*) &pPicture);
if(FAILED(hResult))
{
AfxMessageBox("图形文件装载出错!");
return E_FAIL;
}
OLE_XSIZE_HIMETRIC hmWidth;//图片的真实宽度
OLE_YSIZE_HIMETRIC hmHeight;//图片的真实高度
pPicture->get_Width(&hmWidth);
pPicture->get_Height(&hmHeight);
//将图形输出到屏幕上
hResult = pPicture->Render(hDC, 0, 0, nScrWidth, nScrHeight, 0, hmHeight, hmWidth, -hmHeight, NULL);
CloseHandle(hFile);//关闭打开的文件
pPicture->Release();
// Free memory.
::GlobalFree(hGlobal);
if (SUCCEEDED(hResult))
{
return S_OK;
}
else
{
AfxMessageBox("图形文件装载出错!");
return E_FAIL;
}
}
void CIPictureDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
static char szFilter[] = "JPG Files (*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*||";
CFileDialog dlg(TRUE, "BMP", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
if(dlg.DoModal() != IDOK)
return;
CRect rect;
GetDlgItem(IDC_STATIC)->GetWindowRect(rect);
DisplayPic(dlg.GetPathName(),GetDlgItem(IDC_STATIC)->GetSafeHwnd(), rect.Width(), rect.Height());
}
GDI好像只能显示BMP,如果硬要用GDI显示的话就要用流了,那个蛮麻烦的,
其实用GDI+来显示的话,只用调个函数就行了,蛮方便的,关键是看编绎器支不支持,不支持还得装GDI+
用CImage类,load,bitblt。一共不超过10行代码
vc加载图片网上代码多了去了,google去吧