In the top of form1 i did
WebBrowser webbrowser1;
Then in the constructor
webbrowser1 = new WebBrowser(); webbrowser1.Navigate("http://www.ims.gov.il/IMS/tazpiot/RainRadar.htm");
webbrowser1.DocumentCompleted += webbrowser1_DocumentCompleted;
Then in the DocumentCompleted event
void webbrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.AbsoluteUri != webbrowser1.Url.AbsoluteUri)
{
return;
}
IHTMLDocument2 doc = (IHTMLDocument2)webbrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
foreach (IHTMLImgElement img in doc.images)
{
imgRange.add((IHTMLControlElement)img);
imgRange.execCommand("Copy", false, null);
using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
bmp.Save(@"E:\newimages\" + img.nameProp);
}
}
}
I'm getting on the hard disk 20 images saved. But not the one i needed. The one i need to save from this page in the webBrowser is the image in this link: http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=
The radar it self image. I can make save as... And i can use like i'm doing today WebClient... But WebClient is not working all the time. So i'm trying to surf to the site and get the image of the radar from the webBrowser. But without success.
UPDATE
This is my webclient how i'm downloading the radar image today:
private void fileDownloadRadar()
{
if (Client.IsBusy == true)
{
Client.CancelAsync();
}
else
{
try
{
Client.DownloadFileAsync(myUri, Path.Combine(combinedTemp));
}
catch(Exception errors)
{
string errors2 = errors.ToString();
}
}
}
Client is WebClient
Now i'm going to the directory of where it was downloaded combinedTemp and i see that the downloaded file is 0kb empty.
The url in myUri is:
http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=
When i put this link in my chrome and surf to it i see the latest radar image but when i'm looking in my hard disk it's 0kb the downloaded file.
And i will add this is not happening now but some time ago some months ago i had another problem in some cases not all the time but in many times when i downloaded the image i had another error i even have open question about it one year ago : Why i'm getting exception: Too many automatic redirections were attempted on webclient?
So in general it's two problems but now the problem is that the downloaded file is 0kb.
This is the Client completed event:
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
try
{
client_Error_counter = client_Error_counter + 1;
if (client_Error_counter == 5)
{
Logger.Write("Tried to download radar image file " + client_Error_counter + " times without success");
Logger.Write("Error Tried to download radar image file : " + e.Error);
client_Error_counter = 0;
}
else
{
Client.DownloadFileAsync(myUri, Path.Combine(combinedTemp));
Logger.Write("Times tried to download radar image file: " + client_Error_counter);
Logger.Write("Error Tried to download radar image file : " + e.Error);
}
}
catch(Exception errors)
{
string myerr = errors.ToString();
}
}
And i see now i missed it that when it's getting to the DownloadFileCompleted event there is an error:
e.Error show: e.Error = {"Too many automatic redirections were attempted."}
StackTRace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
This is why i tried to use the WebBrowser. I can't find a way to solve this "Too many automatic redirections were attempted" problem.
That's why i see the file 0kb on my hard disk.
Another thing is today few hours ago it did download fine some radar images but now it's making this error again.