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;
        }
    }
}

 




Comments are closed.