16,550
社区成员
发帖
与我相关
我的任务
分享int COrcDlg::GetBmp()
{
HINTERNET internetopen =NULL;
internetopen=InternetOpen("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; InfoPath.1)",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if (internetopen==NULL)
{
if(NULL != internetopen) InternetCloseHandle(internetopen);
return -1;
}
HINTERNET hConnection;
hConnection= InternetConnect(internetopen,SERVER,INTERNET_DEFAULT_HTTP_PORT,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
if (NULL == hConnection)
{
if(NULL != hConnection) InternetCloseHandle(hConnection);
return -1;
}
HINTERNET internetopenurl = NULL;
char szHeader[]= "Content-Type: application/x-www-form-urlencoded\r\n";
char cookie[] = "Cookie: ASP.NET_SessionId=yclxwe55xyfzwi453brbrv55";
internetopenurl = HttpOpenRequest(hConnection,"GET","/xxx/xxx/ValidateCodeImgShow0.3201143178969629.aspx","HTTP/1.1",NULL,0,INTERNET_FLAG_RELOAD| INTERNET_FLAG_NO_COOKIES,0);
if (NULL != internetopenurl)
{
if (TRUE == HttpAddRequestHeaders (internetopenurl,szHeader,strlen(szHeader),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))
{
if(TRUE ==HttpAddRequestHeaders (internetopenurl,cookie,strlen(cookie),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))
if(TRUE == HttpSendRequest(internetopenurl,NULL,0,NULL,NULL))
{
IStream* pStmBmp = NULL;
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, 1024);
if (hMemBmp != NULL)
{
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
if (pStmBmp == NULL)
{
GlobalFree(hMemBmp);
return FALSE;
}
}
while(1)
{
DWORD byteread=0;
BOOL internetreadfile;
char buffer[1024];
memset(buffer,0,sizeof(buffer));
internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);
if(byteread==0)
break;
ULONG pcbWritten;
pStmBmp->Write(buffer,sizeof(buffer),&pcbWritten);
}
Bitmap pic(pStmBmp,NULL);
Gdiplus::Color clr(0xFF,0xFF,0xFF); // (only blue component gets used)
HBITMAP bmp;
if (Gdiplus::Ok == pic.GetHBITMAP(clr, &bmp))
{
m_bmp.SetBitmap(bmp);
::DeleteObject((HGDIOBJ)bmp); //这个地方必须要,不然就会内存泄漏
}
pStmBmp->Release();
GlobalUnlock(hMemBmp);
GlobalFree(hMemBmp);
//AfxMessageBox(strsn.c_str());
InternetCloseHandle(internetopenurl);
InternetCloseHandle(internetopen);
//
}
}
}
else
{
if(NULL != internetopenurl) InternetCloseHandle(internetopenurl);
if(NULL != internetopen) InternetCloseHandle(internetopen);
return -1;
}
return 1;
}