1

I'm trying to put icons on a tab. I'm getting the icon from http://google.com/favicon.ico for now.

I want to get the favicon.ico as System.Drawing.Icon. The original code I'm using is for a normal image, but I need it to be System.Drawing.Icon.

Here's my code so far:

var iconURL = "http://" + url.Host + "/favicon.ico";

System.Drawing.Icon img = null;
WebRequest request = WebRequest.Create(iconURL);
WebResponse response = request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
    img = new System.Drawing.Icon(stream);
    // then use the image
}

qTabControl1.ActiveTabPage.Icon = img;

This gives me the following error:

enter image description here

GoRoS
  • 5,183
  • 2
  • 43
  • 66
PaddiM8
  • 175
  • 1
  • 1
  • 8

1 Answers1

0

You need to copy it to a stream that supports seeking, here is some sample code (using the newer HttpClient and async/await):

async Task<Icon> GetIcon()
{
    var httpClient = new HttpClient();
    using (var stream = await httpClient.GetStreamAsync(url))
    using (var ms = new MemoryStream())
    {
        stream.CopyTo(ms);
        ms.Seek(0, SeekOrigin.Begin); // See https://stackoverflow.com/a/72205381/640195

        return new Icon(ms);
    }
}
yoel halb
  • 12,188
  • 3
  • 57
  • 52