Domanda

I'm reading a html using the webbrowser component in WPF. I should let the user to give there own text, upload an image and save it as a .html file.

By this way I can get the tag value:

 string myTagValue = (FrameWithinGrid.Document as mshtml.HTMLDocument)
             .getElementsByTagName("div")
             .OfType<mshtml.IHTMLElement>()
             .First()
             .innerText;

How can i get the value from a text box and save it as a .html file including a new image?

Need your help. Thank you.

This is the html file:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>Welcome to Linguini Party!</title>
<style type="text/css">
* {
    margin: 0;
}

html, body {
    height: 100%;
    font-family: Arial;
    background-color: #e1dfe3;
    color: #333333;
    background-repeat: no-repeat;
}

h1 {
    margin-right: 8%;
    margin-left: 7%;
    font-size: 450%;
    text-align: center;
}
h2 {
    font-size: 450%;
    text-align: center;
    color: #8a00ca;
    font-style: italic;
}
</style>
</head>

<body id="Imagebg" background="welcome_final1.png">
<div id="container">
<h1 id="text1">Welcome to<br></h1>
<h2 id="text2">Linguini Party!</h2>
</div>
</body>
</html>
È stato utile?

Soluzione

One strategy for accomplishing this is to create an html template along the lines of this...

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body id="Imagebg" >
    <div id="container">
        <h1 id="text1">$$TEXT1$$<br></h1>
        <h2 id="text2">$$TEXT2$$</h2>
        <img src="$$IMAGELOCATION$$"/>
    </div>
</body>
</html>

This template has been instrumented with markers such as $$TEXT1$$. These will be replaced with customized text later.

Include the template as a resource, or as an external file, or wherever. I opt for including it as an embedded resource. Here's the settings...

enter image description here

You don't HAVE to use an embedded resource, you can use the WebBrowser to get the html also. I like using a resource because it avoids exceptions caused by missing or corrupt files.

The next step is easy. Here's a working View Model...

public class ViewModel
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
    public string ImagePath { get; set; }
    public string DestinationName { get; set; }
    public void Start()
    {
        var resourceName = Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(q => q.Contains("Template.html")).FirstOrDefault();
        using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string html = reader.ReadToEnd().Replace("$$TEXT1$$", Text1).Replace("$$TEXT2$$", Text2).Replace("$$IMAGELOCATION$$", ImagePath);
                File.WriteAllText(DestinationName, html);
            }
        }
    }
}

The View Model gets the various Text1 (etc) properties set by the caller, and then loads the HTML template and replaces the markers with custom content. Then it uses the static method on File to save it.

You can modify this strategy to use any other type of instrumentation as long as it's consistent. For a more scalable, full-blown solution, I suggest HtmlAgility. I have been using it for years.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top