Cyotek Forums

Source Code => Source Code => Topic started by: romeroa on April 07, 2017, 03:36:10 PM

Title: Load Image from an URL
Post by: romeroa on April 07, 2017, 03:36:10 PM
Is it possible to load an image (jpg i.e.) directly from an URL?
Title: Re: Load Image from an URL
Post by: Richard Moss on April 07, 2017, 05:00:31 PM
Hello,

It's not something the ImageBox will do naively, but it is easy enough to add yourself. I've updated the demonstration program on GitHub (https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox) to show how to do this, appropriate code below.

try
{
  using (WebClient client = new WebClient())
  {
    byte[] data;

    data = client.DownloadData(url);

    using (Stream stream = new MemoryStream(data))
    {
      this.OpenImage(Image.FromStream(stream));
    }
  }
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}


Regards;
Richard Moss
Title: Re: Load Image from an URL
Post by: romeroa on April 07, 2017, 11:47:44 PM
Thanks.
Again, you help me. Working fine.