Paste the screenshot in Web browser control in windows app using c#

nataraju a 21 Reputation points
2021-09-27T05:57:42.893+00:00

I want to paste the Screen shot or copy the image from disc to web browser control in windows app.

private mshtml.IHTMLDocument2 htmlDoc2;
webBrowser2.DocumentText = "<html><body></body></html>";
htmlDoc2 = webBrowser2.Document.DomDocument as IHTMLDocument2;
htmlDoc2.designMode = "On";

Above code is using to enable write, i can able to paste text contents

Developer technologies | Windows Forms
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-09-27T21:11:37.043+00:00

    [cannot post code in comments...]

    To include the image in DocumentText, you can encode it into Base64

    For example :

                var img = System.Windows.Forms.Clipboard.GetImage();
                if (img != null)
                {
                    string sTempPath = System.IO.Path.GetTempPath();
                    sTempPath = String.Concat(sTempPath, "ClipTemp.jpg");
                    img.Save(sTempPath, System.Drawing.Imaging.ImageFormat.Jpeg);            
    
                    string sStringBase64 = null;
                    using (var fs = new System.IO.FileStream(sTempPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    {
                        var sBuffer = new byte[fs.Length];
                        fs.Read(sBuffer, 0, (int)fs.Length);
                        sStringBase64 = Convert.ToBase64String(sBuffer);
                    }
                    var sDocumentText = String.Format(@"<html>
                                                <body>
                                                    <img src=""data:image/jpg;base64,{0}""/>
                                                </body>
                                            </html>", sStringBase64);
                    webBrowser1.DocumentText = sDocumentText;
                }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2021-09-27T11:10:33.777+00:00

    If you want to copy a screenshot into WebBrowser, you can do (with webBrowser1 for the test) :

    string sTempPath = System.IO.Path.GetTempPath();
    sTempPath = String.Concat(sTempPath, "ClipTemp.jpg");
    var img = System.Windows.Forms.Clipboard.GetImage();
    if (img != null)
    {
        img.Save(sTempPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        webBrowser1.Navigate(sTempPath);
    }
    

  2. nataraju a 21 Reputation points
    2021-09-28T08:10:50.907+00:00

    i did without creating file.

            var pic = System.Windows.Forms.Clipboard.GetImage();
            MemoryStream stream = new MemoryStream();
            if (pic != null)
            {
                pic.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            string imgstr = Convert.ToBase64String(stream.GetBuffer());
            var srctxt = String.Format(@"data:image/jpg;base64,{0}", imgstr);
            if (webBrowser1.Document != null)
            {
                HtmlElement elem = webBrowser1.Document.CreateElement("img");
                elem.SetAttribute("src", srctxt);
    
                webBrowser1.Document.Body.AppendChild(elem);
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.