Title: Download the Astronomy Picture of the Day in C#
Each day the Astronomy Picture of the Day web page does exactly what you would expect: it displays a different astronomy picture. The pictures include planets, nebulae, spacecraft, and other astronomy-related images, and the page includes an explanation of the picture.
Long ago I had written a program to download the Astronomy Picture of the Day but at some point it stopped working because of the way that site managed its security. The images had been stored with normal http URLs but now are stored with https URLs. This example shows how to download the images with the new URL format.
When the program loads, the following code executes.
// Download the Astronomy Picture of the Day.
private void Form1_Load(object sender, EventArgs e)
Cursor = Cursors.WaitCursor;
wbrApotd.Visible = false;
const string url = "http://antwrp.gsfc.nasa.gov/apod/";
// Load the web page.
catch (Exception ex)
"Error navigating to " + url);
This code stores the Astronomy Picture of the Day web site URL in a variable. It then calls the form's WebBrowser control's Navigate method to make the control download the web page.
When the web page has finished loading, the WebBrowser fires its DocumentCompleted event and the following code executes.
// The web page has loaded. Get the APOTD image.
private void wbrApotd_DocumentCompleted(object sender,
HtmlDocument doc = wbrApotd.Document;
string src = doc.Images.GetAttribute("src");
Image img = GetPicture(src);
picApotd.Image = img;
Cursor = Cursors.Default;
This code gets the loaded web page's HtmlDocument object. It finds the document's first Image object and uses its GetAttribute method to get the image's src property, which is the URL where the image is located. It calls the GetPicture method to download the image from it URL and displays it in the PictureBox named picApotd.
The following code shows the GetPicture method.
// Download a file from the internet.
// Get the picture at a given URL.
private Image GetPicture(string url)
WebClient web_client = new WebClient();
// Use one of the following.
MemoryStream image_stream =
catch (Exception ex)
MessageBox.Show("Error downloading picture " +
url + '\n' + ex.Message);
This method creates a new WebClient. It then sets ServicePointManager.SecurityProtocol to 3072. That tells the server to use the TLS1.2 security protocol. Unfortunately that protocol isn't part of the SecurityProtocolType enumeration in .NET Framework versions before 4.5 and this example was written in C# 2008. To work around that, the code casts the value 3072 into the SecurityProtocolType type.
If you are using a more recent version of the .NET Framework, use the commented out value Tls12.
The code then uses the client's DownloadData method to fetch the URL target as a data stream, uses it to create an image, and returns the result.
Download the example to experiment with it and to see additional details.