C# Tutorial – How to load images from URL to a Picture Box

Hi, in this tutorial we will show you how to load images from an URL to a picture box in windows forms. So on many of our tutorials we have loaded pictures to a picture box using local resources that were either loaded externally using file/directory location or embedded into the visual studio resources file. In this tutorial we will show you how to load an image directly from a URL and not only that we will have 50 different images link in a text file which will then be fed into the program and load each of them with a click of a button. This will be a quick tutorial so lets get started on it.

Lesson Objectives –

  1. Create a simple windows forms application that reads URLS from a text file
  2. Load each URL from the text file to a picture box
  3. Navigate those pictures using array indexes

Video Tutorial

Download the text file here
Follow the instructions on the video step by step, if you feel that something is not working then, pause the video and try again. You can download the text file in the link above use that in this tutorial.

Source Code –

namespace URL_Picture_Viewer_MOO_ICT
{
    public partial class Form1 : Form
    {

        // created by MOO ICT

        string[] url = File.ReadLines("image_urls.txt").ToArray();
        int i = 0;

        public Form1()
        {
            InitializeComponent();
            i = url.Length - 1;
            pictureBox1.Load(url[i]);
            label1.Text = "Image " + (i + 1) + " of " + url.Length;

        }

        private void ChangeImageEvent(object sender, EventArgs e)
        {
            if (i > 0)
            {
                i--;
            }
            else
            {
                i = url.Length - 1;
            }

            pictureBox1.Load(url[i]);
            label1.Text = "Image " + (i + 1) + " of " + url.Length;
        }
    }
}

This C# code defines a Windows Forms application within the namespace “URL_Picture_Viewer_MOO_ICT”. Let’s break it down:

The Form1 class inherits from the Form class, provided by the Windows Forms framework. This class represents the main form of the application.

The code initializes an array of strings named “url” by reading lines from a file named “image_urls.txt”. Each line of the file is stored as an element in the “url” array.

An integer variable “i” is declared and initialized to the length of the “url” array minus 1. This variable will be used to keep track of the current index when displaying images from the URLs.

The constructor of Form1 initializes the visual components of the form using the InitializeComponent() method. Additionally, it sets the variable “i” to the last index of the “url” array and loads the corresponding image into a PictureBox control named “pictureBox1”. The label “label1” is updated to display information about the currently displayed image.

The method “ChangeImageEvent” serves as an event handler for changing the displayed image. When triggered (likely by clicking a button or control), it decrements the value of “i” if it is greater than 0, effectively moving to the previous URL in the “url” array. If “i” becomes less than 0 (indicating that the first URL has been reached), “i” is reset to the last index of the “url” array. The image corresponding to the updated URL is loaded into “pictureBox1”, and “label1” is updated to reflect the current image number and the total number of images available.

In summary, this code creates a picture viewer application that loads and displays images from URLs stored in a text file. Users can navigate through the images by clicking a button or control, which triggers the “ChangeImageEvent” method to update the displayed image and its corresponding information.




Comment on this tutorial and let us know how you got on -