Triggers JavaScript in the HTML page in the WebBrowser.

Source: Internet
Author: User
Tags base64

The function of an instance is to add a picture to the HTML img tag.

You can encode the image in base64. For example

<src= "Data:image/gif;base64,myimagedataencodedinbase64
= " alt=" My Image data in base "/>

Here are a full example of what can accomplish this:

usingSystem;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms;namespaceimageencodedinbase64inawebbrowser{[ComVisible (true)]     Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {            stringurl = directory.getcurrentdirectory () +"\\page.html"; Webbrowser1.url=NewUri (URL); Webbrowser1.objectforscripting= This; }        Private voidButton1_Click (Objectsender, EventArgs e) {            stringImageInBase64 =ReadImageInBase64 (); WebBrowser1.Document.InvokeScript ("Setimagedata",New[] {imageInBase64}); }        Private stringReadImageInBase64 () {stringImagePath = directory.getcurrentdirectory () +"\\opensource.png"; using(varFS =NewFileStream (ImagePath, FileMode.Open, FileAccess.Read)) {                varBuffer =New byte[FS.                Length]; Fs. Read (Buffer,0, (int) fs.                Length); returnconvert.tobase64string (buffer); }        }    }}

And this Javascript code:

function Setimagedata (imageBase64) {    var myimg = document.getElementById ("myimg");     = "Data:image/png;base64," + imageBase64;}

Triggers JavaScript in the HTML page in the WebBrowser.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.