从URL路径装入一幅图片

    这个方法实际上是使用IXMLHTTPRequest接口完成图片下载,然后再将下载的数据转换为IStream接口,最后用接口IPicture装一段数据流来完成图片的加载。

 

 

<textarea cols="73" rows="19" name="code" class="cpp">HANDLE LoadImageFromURL(LPCTSTR pszURL) { HRESULT hr; IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL; IStream *pIStream = NULL; IPicture* pIPicture=NULL; VARIANT vValue; HANDLE hTemp, hImage=NULL; try { hr = pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.3.0"); if( FAILED(hr) ) _com_issue_error(hr); hr = pIXMLHTTPRequest-&gt;open(TEXT("GET"), pszURL, false); if( FAILED(hr) ) _com_issue_error(hr); hr=pIXMLHTTPRequest-&gt;send(); if( FAILED(hr) ) _com_issue_error(hr); hr = pIXMLHTTPRequest-&gt;get_responseStream(&amp;vValue); if( FAILED(hr) ) _com_issue_error(hr); pIStream = (IStream*)vValue.punkVal; hr = OleLoadPicture(pIStream, 0, FALSE, IID_IPicture, (void**)&amp;pIPicture); if( FAILED(hr) ) _com_issue_error(hr); hr = pIPicture-&gt;get_Handle((OLE_HANDLE*)&amp;hTemp); if( FAILED(hr) ) _com_issue_error(hr); hImage = CopyImage(hTemp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); pIPicture-&gt;Release(); pIStream-&gt;Release(); } catch(...) { if(pIStream) pIStream-&gt;Release(); if(pIPicture) pIPicture-&gt;Release(); return NULL; } return hImage; }</textarea>

你可能感兴趣的:(从URL路径装入一幅图片)