Can't show Images from URL or download data in WinForms app (Visual Studio)

SSB 81 Reputation points
2022-05-11T12:55:33.183+00:00

Hi,
I have a new WinForms project:

  • Visual Studio 2019 16.11.13
  • .NET 5
  • Windows 10

I want to load an image from a URL. When I run the app, nothing shows up. I've tried the following:

pic1.LoadAsync(url);
pic1.ImageLocation = url;

Both of these just result in a placeholder image coming back.

I've also tried downloading the stream - but it is getting stuck

using (WebClient webClient = new WebClient())

 {
 byte[] data = webClient.DownloadData(url);


    using (MemoryStream mem = new MemoryStream(data))
    {
        using (var yourImage = Image.FromStream(mem))
        {

        }
    }
 }

Any ideas how to solve for this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,061 Reputation points
    2022-05-11T14:40:18.223+00:00

    What is the URL ?

    If I test with one of my test images, in the constructor, with a PictureBox pictureBox1:

            public Form1()  
            {  
                InitializeComponent();  
                pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;  
                pictureBox1.ImageLocation = "https://i.ibb.co/DG7XSsB/Butterfly.png";  
            }  
    

    I get the image :

    201019-picture-imagelocation.jpg


0 additional answers

Sort by: Most helpful