How do i load html file in WebBrowser? - c#

I have on my hard disk an html file i saved the content from a website with Webclient.
private void DownloadHtml()
{
using (var client = new WebClient())
{
client.DownloadFile(webSite, OriginalHtmlFilePath);
}
}
Now after did some changes with the file content changed only some texts no tags or any scripts i want to load back the html file. So i did:
string html = File.ReadAllText(ScrambledHtmlFilePath);
Uri Uri = new Uri(ScrambledHtmlFilePath);
//webBrowser1.DocumentText = html;
webBrowser1.Navigate(Uri);
In both cases using the html or the Uri its loading the html as local file and therefore im getting some scripts errors.
If i open the file from my hard disk with Chrome or IE it's loading the file online like i surfed to the site im not getting any script errors.
The problem is that when im using Chrome or IE its taking like 10-15 seconds untill its loading the file.
How can i load the html file in WebBrowser fast and to be online like if i was openning it with IE or Chrome ?

You can set the DocumentText property of the WebBrowser control to the edited-HTML content.

Related

How to save string content to a local file

I am developing an application which is showing web pages through a web browser control.
When I click the save button, the web page with images should be stored in local storage. It should be save in .html format.
I have the following code:
WebRequest request = WebRequest.Create(txtURL.Text);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
html = sr.ReadToEnd();
}
Now string html contains the webpage content. I need to save this into D:\Cache\
How do i save the html contents to disk?
You can use this code to write your HTML string to a file:
var path= #"D:\Cache\myfile.html";
File.WriteAllText(path, html);
Further refinement: Extract the filename from your (textual) URL.
Update:
See Get file name from URI string in C# for details. The idea is:
var uri = new Uri(txtUrl.Text);
var filename = uri.IsFile
? System.IO.Path.GetFileName(uri.LocalPath)
: "unknown-file.html";
you have to write below code on save button
File.WriteAllText(path, browser.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(browser.Document.Encoding));
Now the 'Body.parent' must save whole the page instead of just saving only part.
check it.
There is nothing built-in to the .NET Framework as far I know.
So my approach would be like below:
Use System.NET.HttpWebRequest to get the main HTML document as a
string or stream (easy). (Which you have done already)
Load this into a HTMLAgilityPack document where you can now easily
query the document to get lists of all image elements, stylesheet
links, etc.
Then make a separate web request for each of these files and save
them to a subdirectory.
Finally update all relevent links in the main page to point to the
items in the subdirectory.

Download PDF file from WebPage WatIn

I have a website (Bank WebSite) i using WatIn to Login and getting to page with links(with pdf files), each link open a Page with opened pdf.file,on that page i have only the opened pdf file and button to download this file(no need to click on it because page automaticlly popUp message with save\saveAs)
I tried:
1- string page=browser.body.OuterHtml
Not working i cant see the Iframe,i cant find it too.
2-int response = URLDownloadToFile(0, Link, FullFilePath, 0, 0);
Not working a gettin login page it because i need cookies
3- WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(myStringWebResource,fileName);
Gives me the same result.
I CAN'T GET COOKIES FROM WatIn Browser and SET IT IN WebClient
CookieCollection cookies = _browser.GetCookiesForUrl(new Uri(url));
string cookies= ie.Eval("document.cookie");
returns my only 1 parameter
sow please do not say to me that i just need to get cokies from WatIn and set it in myWebClient.
Sow any ideas how can i save this pdf file?
One option would be using iTextSharp library, which would give a list helpful methods to download the PDF. Sample code is below...
Uri uri = new Uri("browser url");
PdfReader reader = new PdfReader(uri);

HiQPdf - Images missing in pdf

I want to convert an html string to pdf with images located at a specific location. I have the html code loaded into a string. I have the images that go with the html located in c:\temp\ . I can get this to work if I save the html string to a file as seen in the example below. I really dont need to write the html to a file. Does anyone have any ideas? I'm using the 3rd party component HiQPDF and I have already contacted them.
private void ConvertToPDF(string htmlbody, string pdfname)
{
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set a demo serial number
htmlToPdfConverter.SerialNumber = "-- HiQPdf Serial Number --";
htmlToPdfConverter.Document.Margins = new PdfMargins(5);
//doesnt work
htmlToPdfConverter.ConvertHtmlToFile(htmlbody, #"c:\temp\", pdfname);
//works just fine :-( , but it forces me to save to the disk
htmlToPdfConverter.ConvertUrlToFile(#"C:\temp\1Z7039680342477761-00a26b62-f6d3-47e3-92c3-18b907665aaa.html", pdfname);
return;
}
Set the base URL as file:///C:\temp/ instead of "c:\temp". This information comes directly from hiqpdf support.

Converting aspx page to image file

I have c# dynamic aspx page after new property add I create for record brochure
http://veneristurkey.com/admin/Brochure.aspx?Admin=PropertiesBrochureA4&id=36
but I want to this convert image file I am searching on internet but all with webbrowser and windows forms. I need on page load show not css type also image file. jpg, png or tiff how i can do this. i need to see sample code..
saving aspx page into an image 2
As I mentioned in my comment, your best bet is to opt for attempting to render HTML to an image.
Here is the link for a library that will allow your to render html to an image:
http://htmlrenderer.codeplex.com/
Here is code that does exactly what you're asking:
http://amoghnatu.wordpress.com/2013/05/13/converting-html-text-to-image-using-c/
Now all you have left is to get the html, since I'm assuming you don't want this to render to the browser prior to generating this image - you should look into grabbing the rendered html from the aspx page on the server prior to returning it, and then just return the image. To render a page:
https://stackoverflow.com/a/647866/1017882
Sorted.
If you do not mind using a commandline tool you can have a look at wkhtmltopdf. The package include a wkhtmltoimage component that can be used to convert HTML to image, using
wkhtmltoimage [URL] [Image Path]
Codaxy also wrote a wkhtmltopdf c# wrapper available through the NuGet package manager. I'm not sure if the wkhtmltoimage component was included, but it should be easy enough to figure out how they wrap the wkhtml components.
i fixed my problem with screenshot machine API they are my code..
public void resimyap()
{
var procad = WS.Satiliklars.Where(v => v.ForSaleID == int.Parse(Request.QueryString["id"])).FirstOrDefault();
var imageBytes = GetBytesFromUrl("http://api.screenshotmachine.com/?key=xxxxxx&size=F&url=http://xxxxxxx.com/a4.aspx?id=" + procad.ForSaleID);
string root = Server.MapPath("~/");
// clean up the path
if (!root.EndsWith(#"\"))
root += #"\";
// make a folder to store the images in
string fileDirectory = root + #"\images\a4en\";
// create the folder if it does not exist
if (!System.IO.Directory.Exists(fileDirectory))
System.IO.Directory.CreateDirectory(fileDirectory);
WriteBytesToFile( fileDirectory + + procad.ForSaleID + ".png", imageBytes);
Yes i also try wkhtmltopdf c# wrapper but in pdf or image converting time my computer fan goin crayz. also i must upload server exe file and my hosting firm didnt support them

How to load HTML/JavaScript from embedded resource into winform web browser

I want to have some HTML files with JavaScript loaded into the web browser control in a winforms (v2.0) application. During execution, I won't have internet access, so JavaScript and HTML forms will be embedded in he resources.resx file.
1) how can I load an HTML document out of the resource (analogous to a file:/// operation, but it isn't being loaded from the file system),
2) how would I declare the JavaScript scripts to be loaded? I.e.,
<script src=resource.jquery.min.js??? ... />
Thanks!
To load the HTML document, just compile your html file as embedded resource, and then:
WebBrowser browser = new WebBrowser();
browser.DocumentText = Properties.Resources.<your_html_file>;
If you really need to have external .js files, I think you will probably need to make them embedded resources. Then you can read these resources into a string of javascript.
string GetResourceString(string scriptFile)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream str = assembly.GetManifestResourceStream(scriptFile);
StreamReader sr = new StreamReader(str, Encoding.ASCII);
return sr.ReadToEnd();
}
(Adapted from a reply on this page)
From here, look into IHTMLScriptElement. From what I understand, you may be able to use this javascript string and set it as the ITHMLScriptElement's text field. See this question
Good luck.
Here's the file structure.
I had success by doing this:
Set the properties of the html files in my solution like this:
Build Action -> Content
Copy to Output Directory -> Copy always
Configure my webBrowser object properties like this:
var myAssembly = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
var path = myAssembly.Substring(0, myAssembly.Length - "MyLib.DLL".Length) + "WebViews/prototype/index.html";
webBrowser.Url = new Uri(path);

Categories