How to Read and display HTML file in Windows form application ~ MiTechDev.com

Mitechdev.com is a programming blog which focused on Web Programming and development

Monday, 30 October 2017

How to Read and display HTML file in Windows form application

07:55:00 Posted by raviteja swayampu , No comments
In this tutorial Raviteja Swayampu explained about how to read and display HTML file in windows forms application.In present windows stand alone applications you can see different links that redirects us to different websites and shows some images and video contents.Best example for this is Visual studio start page.

Featured Videos
News Feed

If you observe this images you can easy find that those are html links.When we opens visual studio it fetches the html file from server.In current tutorial we will see how to implement this by using WebBrowser control in windows forms application.

Read and Display HTML file in Windows form

1.Create window form application in visual studio.
2.Prepare a html file with image and some text to input for windows form application.
sample.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title></title>
</head>
<body>
    <p>A famous quote of steev is</p>
    <hr />
    <img src='https://pi.tedcdn.com/r/pe.tedcdn.com/images/ted/0ef62e4df27b4ba7294de889fdbc33e476a08ec9_254x191.jpg' />
</body>
</html>

3.Namespaces need to import for this application is as follows
Imports System.IO
Imports System.Reflection

4.Write below code in Form_Load method.
private void Form1_Load(object sender, EventArgs e)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("WebBrowser_HTML_File_CS.sample.htm"));
            webbrowser.DocumentText = reader.ReadToEnd(); 
        }


5.If you observer the above code the html file is loaded into StreamReader object  using  GetManifestResourceStream method.The contents of stram reader are read using ReadToEnd method and assigned to WebBrowser control to display html file in windows forms application.
6.Final output of the application is look like below.


7.Download the source code for this application from here Download.


0 comments:

Post a Comment