Post by bobowk on Jan 14, 2017 10:46:23 GMT 10
System.ArgumentException: 'Value of 'null' is not valid for 'stream'.' This is an error I've encountered trying to get a favicon. Here is my code:
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (urlTextBox.Text.Contains("about:blank"))
{
}
else
{
var url = new Uri("http://" + urlTextBox.Text + ".com");
if (url.HostNameType == UriHostNameType.Dns)
{
var iconURL = "http://" + url.Host + "/favicon.ico";
System.Net.WebRequest request = System.Net.HttpWebRequest.Create(iconURL);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream Stream = request.GetResponse().GetResponseStream();
dynamic favicon = Image.FromStream(stream);
ParentTabs.SelectedTab.Icon = favicon;
ParentTabs.SelectedTab.Icon = null;
}
}
}
|
|
Post by Imformas on Jan 14, 2017 12:16:23 GMT 10
Give this a try void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (urlTextBox.Text.Contains("about:blank")) { } else { var url = new Uri("http://" + urlTextBox.Text + ".com"); if (url.HostNameType == UriHostNameType.Dns) { var iconURL = "http://" + url.Host + "/favicon.ico"; System.Net.WebRequest request = System.Net.HttpWebRequest.Create(iconURL); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream Stream = request.GetResponse().GetResponseStream(); dynamic favicon = Image.FromStream(stream); ParentTabs.SelectedTab.Icon = favicon; favicon = null; } } }
Programming language'sCSharpC++CObjective-CVBProject'sSharpMediaSharpEditYoutifygithub.com/Stephen-Fox-0
|
|
Post by bobowk on Jan 16, 2017 16:53:16 GMT 10
@informas public static Image GetFavicon(string longUrl) { // Input website address var url = new Uri(longUrl);
try { if (url.HostNameType == UriHostNameType.Dns) { // Get the URL of the favicon // url.Host will return such string as www.google.com dynamic iconUrl = "http://" + url.Host + "/favicon.ico";
// Download the favicon WebRequest request = HttpWebRequest.Create(iconUrl); var response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); if (stream != null) { dynamic rawfavicon = Image.FromStream(stream);
Application.DoEvents();
//set the default sizes. normally they are 16 by 16 but in my project 15 by 15 looks better const int width = 15; const int height = 15;
var favicon = new Bitmap(width, height); Graphics g = Graphics.FromImage(favicon);
g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(rawfavicon, new Rectangle(0, 0, width, height), new Rectangle(0, 0, rawfavicon.Width, rawfavicon.Height), GraphicsUnit.Pixel); g.Dispose(); rawfavicon.Dispose(); stream.Dispose();
return favicon; } } } catch (Exception) { return Properties.Resources.document; //you have to make this one your self and put it in resources. }
}
|
|