加载网络图片并显示 OleLoadPicture没取到值

phhui 2016-03-25 02:58:58
代码如下:
断点到代码中标的地方停了,不知道是什么问题,刚接触C++一时找不到解决方法,这是什么原因?或我要怎么处理这个问题?
HDC hDC_Temp = dc->GetSafeHdc();
IPicture *pPic;
IStream *pStm;
HRESULT bResult;// = FALSE;
DWORD dwFileSize, dwByteRead;
//读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(L"HttpClient");
CFile* httpFile = (CFile*)session.OpenURL(url);
if (httpFile != INVALID_HANDLE_VALUE){
dwFileSize = httpFile->GetLength();//获取文件字节数
if (dwFileSize == 0xFFFFFFFF)
return E_FAIL;
}else{
return E_FAIL;
}
//分配全局存储空间
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData = NULL;
if (hGlobal == NULL)
return E_FAIL;

if ((pvData = GlobalLock(hGlobal)) == NULL)//锁定分配内存块
return E_FAIL;

//把文件读入内存缓冲区
dwByteRead = httpFile->Read(pvData, dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
//装入图形文件
bResult = OleLoadPicture(pStm, dwFileSize, TRUE, IID_IPicture, (LPVOID*)&pPic);
if (FAILED(bResult))
return E_FAIL;//*****【【【【【------到这里停止了------】】】】】******

OLE_XSIZE_HIMETRIC hmWidth; //图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight; //图片的真实高度, 单位为英寸
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int nWidth = MulDiv(hmWidth, GetDeviceCaps(hDC_Temp, LOGPIXELSX), 2540);
int nHeight = MulDiv(hmHeight, GetDeviceCaps(hDC_Temp, LOGPIXELSY), 2540);

//缩放图片
int nWZoom = nWidth / rect.Width();
int nHZoom = nHeight / rect.Height();
int nZoom = 1;
nZoom = (nWZoom > nHZoom) ? nWZoom : nHZoom;
nWidth /= nZoom;
nHeight /= nZoom;
int midW = (rect.left + rect.right) / 2;
int midH = (rect.top + rect.bottom) / 2;
rect.left = midW - nWidth / 2;
rect.right = midW + nWidth / 2;
rect.top = midH - nHeight / 2;
rect.bottom = midH + nHeight / 2;
//将图形输出到屏幕上(有点像BitBlt)
bResult = pPic->Render(hDC_Temp, rect.left, rect.top, rect.Width(), rect.Height(),0, hmHeight, hmWidth, -hmHeight, NULL);
pPic->Release();
httpFile->Close();//关闭打开的文件
if (SUCCEEDED(bResult)){
return S_OK;
}else{
return E_FAIL;
}
...全文
137 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cutmelon 2016-03-25
  • 打赏
  • 举报
回复
GlobalUnlock(hGlobal);过早
赵4老师 2016-03-25
  • 打赏
  • 举报
回复
OleLoadPicture Creates a new picture object and initializes it from the contents of a stream. This is equivalent to calling OleCreatePictureIndirect(NULL, ...) followed by IPersistStream::Load. STDAPI OleLoadPicture( IStream * pStream, //Pointer to the stream that contains picture's data LONG lSize, //Number of bytes read from the stream BOOL fRunmode, //The opposite of the initial value of the picture's // property REFIID riid, //Reference to the identifier of the interface // describing the type of interface pointer to return VOID ppvObj //Address of output variable that receives interface // pointer requested in riid ); Parameters pStream [in] Pointer to the stream that contains the picture's data. lSize [in] Number of bytes that should be read from the stream, or zero if the entire stream should be read. fRunmode [in] The opposite of the initial value of the KeepOriginalFormat property. If TRUE, KeepOriginalFormat is set to FALSE and vice-versa. riid [in] Reference to the identifier of the interface describing the type of interface pointer to return in ppvObj. ppvObj [out] Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppvObj contains the requested interface pointer on the storage of the object identified by the moniker. If *ppvObj is non-NULL, this function calls IUnknown::AddRef on the interface; it is the caller's responsibility to call IUnknown::Release. If an error occurs, *ppvObj is set to NULL. Return Values This function supports the standard return values E_OUTOFMEMORY and E_UNEXPECTED, as well as the following: S_OK The picture was created successfully. E_POINTER The address in pStream or ppvObj is not valid. For example, either may be NULL. E_NOINTERFACE The object does not support the interface specified in riid. Remarks The stream must be in BMP (bitmap), WMF (metafile), or ICO (icon) format. A picture object created using OleLoadPicture always has ownership of its internal resources (fOwn==TRUE is implied). QuickInfo Windows NT: Use version 4.0 or later. Windows: Use Windows 95 or later. Windows CE: Unsupported. Header: Declared in olectl.h. Import Library: Included as a resource in olepro32.dll. See Also OleCreatePictureIndirect, PICTDESC

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧