Adding multiple web based images to ImageList

Squatcher

Member
Joined
Mar 1, 2023
Messages
5
Programming Experience
Beginner
Good afternoon,

I want to add a group of web based images to ImageList, so that I can then use the ImageList with a pictureBox to rotate which images using a timer. But I it seems to be having issues.

Any suggestions on where I can find a tutorial or suggestions where I am messing up?
C#:
public void Solar_Image()
        {
            try
            {
                string imageUrl = @"https://sdo.gsfc.nasa.gov/assets/img/latest/latest_512_0094.jpg";
                string saveLocation = @"C:\Users\richa\source\repos\Ham_Radio\latest_512_0094.jpg";

                //string imageUrl2 = @"https://sdo.gsfc.nasa.gov/assets/img/latest/latest_512_0193.jpg";
                //string saveLocation2 = @"C:\Users\richa\source\repos\Ham_Radio\Solar_0193_Image.jpg";

                byte[] imageBytes;

                HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
                WebResponse imageResponse = imageRequest.GetResponse();

                Stream responseStream = imageResponse.GetResponseStream();

                using (BinaryReader br = new BinaryReader(responseStream))
                {
                    imageBytes = br.ReadBytes(500000);
                    br.Close();
                }
                responseStream.Close();
                imageResponse.Close();

                FileStream fs = new FileStream(saveLocation, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                try
                {
                    bw.Write(imageBytes);
                    imageList1SolarImages.Images.Add(Image.FromStream(fs));
                    pbSolarImage.Image = imageList1SolarImages.Images[0];

                }
                finally
                {
                    fs.Close();
                    bw.Close();
                    
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("Error" + ex);
            }
        }

Timer:
private void timer1_Tick(object sender, EventArgs e)
        {
           // pbSolarImage.Image = imageList1SolarImages.Images[0];
            pbSolarImage.Image = imageList1SolarImages.Images[0];

            if (intImgNum == imageList1SolarImages.Images.Count - 1)
            {
                intImgNum = 0;
            }
            else
            {
                intImgNum++;
            }

        }
 
On the surface, just scanning the code, it seems like you never rotate around to the other images in your timer. You are always using the first image.
C#:
pbSolarImage.Image = imageList1SolarImages.Images[0];
 
Since .Net 6 WebRequest is obsolete, it is also not recommended to use for new development for .Net Framework. Breaking change: WebRequest, WebClient, and ServicePoint are obsolete - .NET
Here is an example using the recommended HttpClient:
C#:
private static readonly HttpClient client = new();
private readonly List<Image> imageList = new();

private async Task LoadImage(string url)
{
    var response = await client.GetAsync(url);
    if (response.IsSuccessStatusCode)
    {               
        var filename = Path.GetFileName(url);
        using (var fs = File.Create(filename))
            await response.Content.CopyToAsync(fs);
        imageList.Add(Image.FromFile(filename));
    }
}
 
Back
Top Bottom