Esempio n. 1
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            // display title and credit
            lblTitle.Text = apodResponse.Title;

            // remove newlines & redundant "Image credit"-s from copyright info
            string imageCredit = apodResponse.Copyright.Replace("\n", " ");

            imageCredit = imageCredit.Replace("Image credit: ", "");

            lblCredits.Text = $"Image credit: {imageCredit}";

            // parse, format, and display image date
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}";

            lblDate.Text = formattedDate;

            // display desc
            lblDescription.Text = apodResponse.Explanation;

            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image for {apodResponse}\n{e.Message}");
            }
        }
Esempio n. 2
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            //show title
            lblTitle.Text = apodResponse.Title;

            //format and show image credits
            lblCredits.Text = $"Image credit;  {apodResponse.Copyright}";


            //convert date string from response which is in the form yyy-mm-dd,
            //into a Datetime, so it can be formatted and dsiplayed
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}";

            lblDate.Text = formattedDate;

            //show explanation text
            lblDescription.Text = apodResponse.Explanation;

            //load picture, handle any image loading errors
            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error Loading Image saved for {apodResponse}'\n'{e.Message}");
            }
        }
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            // Show title
            lblTitle.Text = apodResponse.Title;

            // Format and show image credits
            lblCredits.Text = $"Image credit: {apodResponse.Copyright}";

            // Convert date string from response, which is in the form yyyy-mm-dd,
            // into a DateTime, so it can be formatted and displayed
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}"; // Example format "Saturday January 19, 2020"

            lblDate.Text = formattedDate;

            // Show explanation text
            lblDescription.Text = apodResponse.Explanation;

            // Try to load image. Catch and handle any image file errors
            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
        }
Esempio n. 4
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            lblTitle.Text = apodResponse.Title;

            string imageCredits          = apodResponse.Copyright.Replace("\n", " ");
            string lowercaseImageCredits = imageCredits.ToLower();

            if (lowercaseImageCredits.Contains("image credit"))
            {
                lblCredits.Text = apodResponse.Copyright;
            }
            else
            {
                lblCredits.Text = $"Image credit: {imageCredits}";
            }

            DateTime date = DateTime.Parse(apodResponse.Date);

            string formattedDate = $"{date:D}";

            lblDate.Text = formattedDate;

            lblDescription.Text = apodResponse.Explanation;

            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
        }
Esempio n. 5
0
        // TODO: Create new method to display data from an APODResponse in the form.
        private void loadImageResposeIntoForm(APODResponse apodResponse)
        {
            // TODO: Show title in lblTitle
            lblTitle.Text = apodResponse.Title;

            // Format and show image credits in lblCredits
            lblCredits.Text = $"Image credit: {apodResponse.Copyright}";

            // Convert date string from response, which is in the form yyyy - mm - dd, into a DateTime
            //format and display the date string in lblDate
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formatteddate = $"{date:D}"; // example format "Saturday January 19, 2020

            //Show explanation text in lblDescription
            lblDescription.Text = apodResponse.Explanation;

            //  Load the image into the picAstronomyPicture PictureBox.
            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
            // Catch any errors thrown loading the image
        }
Esempio n. 6
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            lblTitle.Text = apodResponse.Title;

            //cleans up copyright formatting.
            string credit = apodResponse.Copyright.Replace('\n', ' ');

            if (credit.Contains("Image credit:"))
            {
                lblCredits.Text = $"{credit}";
            }
            else
            {
                lblCredits.Text = $"Image credit: {credit}";
            }

            // Cleans up the date formatting.
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}";

            lblDate.Text = formattedDate;

            lblDescription.Text = apodResponse.Explanation;

            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
        }
Esempio n. 7
0
        private void HandleResponse(APODResponse apodResponse, string error)
        {
            // TODO: if there is an error from the request, show a MessageBox

            // TODO: Make sure response is an image (not a video or other media type)

            // TODO: If there are no errors, and the response is an image, call a method
            //  (that you'll create) to display the info in the form

            // TODO: if APOD is not an image, display a message box, ask user to try another date
        }
Esempio n. 8
0
 private void apodBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     if (e.Argument is DateTime dt)
     {
         APODResponse apodResponse = APOD.FetchAPOD(out string error, dt);
         e.Result = (reponse : apodResponse, error);
         Debug.WriteLine(e.Result);
     }
     else
     {
         Debug.WriteLine("Background worker error - argument not a DateTime" + e.Argument);
         throw new Exception("Incorrect Argument type, must be a DateTime");
     }
 }
Esempio n. 9
0
 private void apodBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     // If the argument is a DateTime, convert it to a DateTime and store in variable named dt
     if (e.Argument is DateTime dt)
     {
         APODResponse apodResponse = APOD.FetchAPOD(out string error, dt); // Make the request!
         e.Result = (reponse : apodResponse, error);                       // A tuple https://docs.microsoft.com/en-us/dotnet/csharp/tuples
         Debug.WriteLine(e.Result);
     }
     else
     {
         Debug.WriteLine("Background worker error - argument not a DateTime" + e.Argument);
         throw new Exception("Incorrect Argument type, must be a DateTime");
     }
 }
Esempio n. 10
0
        private void HandleResponse(APODResponse apodResponse, string error)
        {
            if (error != null)
            {
                MessageBox.Show(error, "Error");
                return;
            }

            if (apodResponse.MediaType.Equals("image"))
            {
                LoadImageResponseIntoForm(apodResponse);
            }
            else
            {
                MessageBox.Show($"The response is not an image. Please try another date.", "Sorry!");
            }
        }
Esempio n. 11
0
        private void HandleResponse(APODResponse apodResponse, string error)
        {
            if (error != null) // bad response
            {
                MessageBox.Show(error, "Error");
                Debug.WriteLine(error);
                return;
            }

            if (apodResponse.MediaType.Equals("image")) // make sure the response is an image
            {
                LoadImageResponseIntoForm(apodResponse);
            }
            else
            {
                MessageBox.Show("The response is not an image. Try another date.", "Sorry!");
            }
        }
Esempio n. 12
0
        private void HandleResponse(APODResponse apodResponse, string error)
        {
            // method makes sure it got a valid image responce and displays an image or message accordingly.
            if (error != null)
            {
                MessageBox.Show(error, "Error");
                return;
            }

            if (apodResponse.MediaType.Equals("image"))
            {
                LoadImageResponseIntoForm(apodResponse);
            }
            else
            {
                MessageBox.Show($"The response is not an image. Please try another date.", "Error");
            }
        }
Esempio n. 13
0
        // TODO: Create new method to display data from an APODResponse in the form.
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            // TODO: Show title in lblTitle
            lblTitle.Text = apodResponse.Title;
            // TODO: Format and show image credits in lblCredits

            string copyright = apodResponse.Copyright;

            copyright = copyright.Replace("\n", " ");    //fixes error where last name is on a newline, check 04/11/2020 for future debug
            string copyrightLower = copyright.ToLower(); //to lower to identify "image credit" already listed

            if (copyrightLower.Contains("image credit"))
            {
                lblCredits.Text = copyright;
            }
            else
            {
                lblCredits.Text = $"Image credit: {copyright}";
            }


            // TODO: Convert date string from response, which is in the form yyyy-mm-dd, into a DateTime
            // TODO: format and display the date string in lblDate
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}"; //example format "saturday january 19

            lblDate.Text = formattedDate;
            // TODO: Show explanation text in lblDescription
            lblDescription.Text = apodResponse.Explanation;
            // TODO: Load the image into the picAstronomyPicture PictureBox.
            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
            // TODO: Catch any errors thrown loading the image
        }
Esempio n. 14
0
        // TODO: Create new method to display data from an APODResponse in the form.
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            // TODO: Show title in lblTitle
            lblTitle.Text = apodResponse.Title;

            // TODO: Format and show image credits in lblCredits
            string imageCredits          = apodResponse.Copyright.Replace("\n", " ");
            string lowercaseImageCredits = imageCredits.ToLower();

            if (lowercaseImageCredits.Contains("image credit"))
            {
                lblCredits.Text = apodResponse.Copyright;
            }
            else
            {
                lblCredits.Text = $"Image credit: {imageCredits}";
            }

            // TODO: Convert date string from response, which is in the form yyyy-mm-dd, into a DateTime
            DateTime date = DateTime.Parse(apodResponse.Date);

            // TODO: format and display the date string in lblDate
            string formattedDate = $"{date:D}";

            lblDate.Text = formattedDate;

            // TODO: Show explanation text in lblDescription
            lblDescription.Text = apodResponse.Explanation;

            try
            {
                // TODO: Load the image into the picAstronomyPicture PictureBox.
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                // TODO: Catch any errors thrown loading the image
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
        }
Esempio n. 15
0
        private void HandleResponse(APODResponse apodResponse, string error)
        {
            if (error != null)
            {
                MessageBox.Show(error, "Error");
                return;
            }

            // Make sure response is an image (not a video or other media type) before loading
            if (apodResponse.MediaType.Equals("image"))
            {
                LoadImageResponseIntoForm(apodResponse);
            }
            else
            {
                MessageBox.Show($"The response is not an image. Please try another date.", "Sorry!");
            }
            // TODO: If there are no errors, and the response is an image, call a method
            //  (that you'll create) to display the info in the form

            // TODO: if APOD is not an image, display a message box, ask user to try another date
        }
        private void HandleResponse(APODResponse apodResponse, string error)
        {
            // Check if there was an error, show user error message if so
            if (error != null)
            {
                MessageBox.Show(error, "Error");
                return;
            }

            // Does the response represent an image? Some "photos" are videos,
            // which we can't display in this app.
            if (apodResponse.MediaType.Equals("image"))
            {
                // If the response is an image, call method to display
                // the data in the form controls
                LoadImageResponseIntoForm(apodResponse);
            }
            else
            {
                MessageBox.Show($"The response for this date is not an image", "Sorry!");
            }
        }
Esempio n. 17
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            lblTitle.Text = apodResponse.Title;

            lblCredits.Text = $"Image Credit: {apodResponse.Copyright}";

            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:d}";

            lblDate.Text = formattedDate;

            lblDescription.Text = apodResponse.Explanation;

            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n{e.Message}");
            }
        }
Esempio n. 18
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            //Show title
            lblTitle.Text = apodResponse.Title;
            string copyright = apodResponse.Copyright;

            copyright = copyright.Replace("\n", " "); // remove newline and replace with blank string or space

            string str = "Image credit";              //Initialze variable s by assigning string value called Image credit

            //Do the same thing with the text "Image credit"
            //Replace "Image credit" with ""
            copyright = copyright.Replace("Image credit", " ");//remove duplicate string and replace  with blank string

            // Format and show image credits
            lblCredits.Text = $"Image credit: {copyright}";

            //Convert date string from response, which is in the form yyyy-mm-dd,
            //into a DataTime, so it can be formatted and displayed
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}"; // Example format "Saturday January 19, 2020"

            lblDate.Text = formattedDate;

            // Show explanation text
            lblDescription.Text = apodResponse.Explanation;


            //Load picture, handle any image loading errors
            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}\n {e.Message}");
            }
        }
Esempio n. 19
0
        private void LoadImageResponseIntoForm(APODResponse apodResponse)
        {
            // Show title
            lblTitle.Text = apodResponse.Title;

            // Format and show image credits
            if (apodResponse.Copyright.Contains("Image credit:"))
            {
                // Solution for the doble 'Image Credit' print out
                lblCredits.Text = $"{apodResponse.Copyright.Replace('\n',' ')}";
            }
            else
            {
                lblCredits.Text = $"Image credit: {apodResponse.Copyright.Replace('\n', ' ')}";
            }

            // Convert date string from response, which is in the form yyyy-mm-dd
            // into a DatTime, so it can be formatted and displayed
            DateTime date          = DateTime.Parse(apodResponse.Date);
            string   formattedDate = $"{date:D}"; // Example format "Saturday January 19, 2020"

            lblDate.Text = formattedDate;

            // Show explanation text
            lblDescription.Text = apodResponse.Explanation;

            // Load picture, handle any image loading errors
            try
            {
                picAstronomyPicture.Image = Image.FromFile(apodResponse.FileSavePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error loading image saved for {apodResponse}/n{e.Message}");
            }
        }
Esempio n. 20
0
        public static APODResponse FetchAPOD(out string errorMessage, DateTime?forDate = null, string imageSavePath = null)
        {
            WebClient client = new WebClient();

            using (client)
            {
                // URL for making APIs requests to APOD service, this requests today's picture
                // Try this URL in a browser, you'll see a response in JSON format
                string url = "https://api.nasa.gov/planetary/apod?api_key=6RVB2VqVPHzfyI9TQ0d4XeQEiAopapJf3RmvapIk";

                try
                {
                    // If a DateTime is provided, format as yyyy-MM-dd for example, 2020-03-30 for March 30th 2020
                    // and append to the URL to request photo for that date
                    if (forDate != null)
                    {
                        string isoDate = $"{forDate:yyyy-MM-dd}";
                        url += $"&date={isoDate}";
                    }

                    Debug.WriteLine("Using URL " + url);

                    // Make request, download JSON response as a string
                    var responseString = client.DownloadString(url);

                    // Configure JSON serializer
                    // use setting to convert JSON lowercase attributes to C# style CamelCase variables
                    var serializerOptions = new JsonSerializerOptions
                    {
                        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                    };

                    // Deserialize - convert the JSON to a custom C# object of type APODResponse.
                    // The properties and values in the JSON are converted to properties and values in the C# object
                    // See also the APODResponse.cs file
                    APODResponse response = JsonSerializer.Deserialize <APODResponse>(responseString, serializerOptions);

                    // If no image save path set, use the temp directory on the computer, with default filename.
                    if (imageSavePath == null)
                    {
                        imageSavePath = Path.Combine(Path.GetTempPath(), "apod.jpg");
                    }

                    // The response contains a URL that points to the actual image. Download to the imageSavePath
                    client.DownloadFile(response.ImageUrl, imageSavePath);

                    // Add the path the image was saved to, to the APODResponse object by setting the FileSavePath property
                    response.FileSavePath = imageSavePath;

                    // For troubleshooting
                    Debug.WriteLine(response);

                    // Everything seems to have worked, set errorMessage to null
                    // and return response containing all the APOD data
                    errorMessage = null;
                    return(response);
                }
                catch (WebException we)
                {
                    // Catch various connectivity problems
                    errorMessage = "Error fetching data from NASA because " + we.Message;
                }
                catch (IOException ioe)
                {
                    // Catch various image save file problems
                    errorMessage = "Error saving image file because " + ioe.Message;
                }
                catch (Exception ex)
                {
                    // And for other things that may go wrong.
                    errorMessage = "Unexpected Exception with message " + ex.Message;
                }

                // For troubleshooting
                Debug.WriteLine($"Error fetching data from APOD server because {errorMessage}");

                return(null);   // Caller will be able to check for a null return value, to test if request was not succesful
            }
        }