Post by bobowk on Jan 16, 2017 19:49:39 GMT 10
I've been having a problem with favicons! This is my problem: I am using:
if (url.HostNameType == UriHostNameType.Dns)
{
dynamic iconURL = "http://" + url.Host + "/favicon.ico";
WebRequest request = System.Net.HttpWebRequest.Create(iconURL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
dynamic favicon = Image.FromStream(stream);
ParentTabs.SelectedTab.Icon = favicon;
} Which doesn't work all the time... The reason you ask? It is because of this: The reason is because the favicon is a bitmap, and my parenttabs only accept icons for the selected tab, thus giving me the error: 'Cannot implicitly convert type 'System.Drawing.Bitmap' to 'System.Drawing.Icon''. This has put a halt to my project for almost 3 hours and I am struggling to fix it.
EDIT: SOLVED!
if (web.Source.HostNameType == UriHostNameType.Dns)
{
WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + web.Source.Host + "/favicon.ico");
Bitmap bm = new Bitmap(32, 32);
MemoryStream memStream;
using (Stream response = request.GetResponse().GetResponseStream())
{
memStream = new MemoryStream();
byte[] buffer = new byte[1024];
int byteCount;
do
{
byteCount = response.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, byteCount);
} while (byteCount > 0);
}
bm = new Bitmap(Image.FromStream(memStream));
if (bm != null)
{
Icon ic = Icon.FromHandle(bm.GetHicon());
ParentTabs.SelectedTab.Icon = ic;
}
}
|
|