C++中使用CAtlHttpClient显示网络图片的方法,支持png,bmp,gif图片格式

示例程序下载地址: http://download.csdn.net/detail/mnorst/4186550

void CShowNetImageView::OnDraw(CDC* pDC)
{
	CShowNetImageDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码


	CAtlHttpClient* client = new CAtlHttpClient();

	if (client->Navigate( _T("http://www.baidu.com/img/baidu_sylogo1.gif") ))
	{
		if (client->GetStatus() == 200) // 200 = successful HTTP transaction
		{
			HGLOBAL hGlobal  =  GlobalAlloc(GMEM_MOVEABLE, client->GetBodyLength());
			void *  pData  =  GlobalLock(hGlobal);
			memcpy(pData, client->GetBody(), client->GetBodyLength());
			GlobalUnlock(hGlobal);
			IStream *  pStream  =  NULL;
			if (CreateStreamOnHGlobal(hGlobal, TRUE,  & pStream)  ==  S_OK)
			{
				CImage image;
				if (SUCCEEDED(image.Load(pStream)))
				{

					image.Draw(pDC->GetSafeHdc(), 0, 0);

				}
				pStream -> Release();    
			}
			GlobalFree(hGlobal);

			CFile file;
			file.Open(_T("D:\\ab.gif"), CFile::modeCreate | CFile::modeWrite );
			file.Write(client->GetBody(), client->GetBodyLength());
			file.Flush();
			file.Close();
		}
	}
}


你可能感兴趣的:(C++,网络)