Retrieving Images off the Web

This is a discussion on Retrieving Images off the Web within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. We just had about 6 inches of rain here last night and it got me thinking about making ...

Retrieving Images off the Web

Hey everyone. We just had about 6 inches of rain here last night and it got me thinking about making a sort of desktop radar. Now, one solution to doing this would be to buy a radar and get extremley local weather for a lot of money, or make a program which regularly goes online and retrieves updated radar images and downloads them. My question is, how do I do this? I'd prefer some sort of thing for MFC, as I'm not very good at normal WinAPI, and console wouldn't be able to display it. Would I have to write my own function? If so, how can I get it to access the webpage and download the images every, say, 5 minutes or so? Thanks!

This should help, MFC only.

Well for MFC I believe you can just make a CAsyncSocket Object then in the class put your prototype functions? (In a class what would you call that?) Anyways its probably best if you learn the WinAPI way however Objects will work as well. Here is a sample of how you would probably download an image:

Code:

class CObjects : public CAsyncSocket
{
public:
virtual void OnClose(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnReceive(int nErrorCode);
};
CObjects MySocket;
void CObjects::OnClose(int nErrorCode)
{
MessageBox(NULL,"They closed the connection!",NULL,MB_OK);
}
void CObjects::OnConnect(int nErrorCode)
{
MessageBox(NULL,"Connected!",NULL,MB_OK);
}
void CObjects::OnReceive(int nErrorCode)
{
MessageBox(NULL,"Data Incoming!",NULL,MB_OK);
//If this is the picture download it and write it to a file using Receive, and WriteFile. Be careful, HTTP server may send unwanted data without asking for it!
}
//Now to connect you first have to create a socket, then connect to the domain.
MySocket.Create(0,SOCK_STREAM,FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); //All you really need in this case is FD_READ, FD_CLOSE, FD_CONNECT.
MySocket.Connect("www.somedomain.com",80); //Port 80 is default HTTP Protocol Port.
//Now you'll want to send data to get your picture!:
You'll have to use MySocket.Send(lpBuf, lpBufLen, 0);
To get a picture I believe you send the following: "GET SomePicture.jpg" and NEVER forget your CRLF's in Data Transfer.
//And when they send you something you'll retreive it through your CObjects::OnReceive function using MySocket.Receive or by simply using Receive.

The above is for MFC only, don't forget to include stdafx.h and winsock.h and you'll probably have to Link to: Ws2_32.lib

Now, one solution to doing this would be to buy a radar and get extremley local weather for a lot of money, or make a program which regularly goes online and retrieves updated radar images and downloads them.