3个回答
展开全部
用CImage类,load,bitblt。一共不超过10行代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-29
展开全部
vc加载图片网上代码多了去了,google去吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你个例子 利用流
工程基于对话框 名字是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());
}
工程基于对话框 名字是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());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询