0

I'm using these 2 Functions for generate Image from Html Code:

    public static void StartBrowser(string HtmlText)
    {
        var th = new Thread(() =>
        {
            var webBrowser = new WebBrowser();
            webBrowser.ScrollBarsEnabled = false;
            webBrowser.IsWebBrowserContextMenuEnabled = true;
            webBrowser.AllowNavigation = true;

            webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
            webBrowser.DocumentText = HtmlText;

            Application.Run();
        });
        th.SetApartmentState(ApartmentState.STA);
        th.Start();
    }
    static void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        string path = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        var webBrowser = (WebBrowser)sender;
        using (Bitmap bitmap =
            new Bitmap(
                webBrowser.Width=450,
                webBrowser.Height=1000))
        {
            webBrowser
                .DrawToBitmap(
                bitmap,
                new System.Drawing
                    .Rectangle(0, 0, bitmap.Width, bitmap.Height));

            string imgfile = "fileimg" + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".jpg";
            bitmap.Save(path + "/images/" + imgfile, System.Drawing.Imaging.ImageFormat.Jpeg);
            OrderDetailRepeatedImage = path + "/images/" + imgfile;

        }
        Application.ExitThread();

    }

The Issue that I am trying to fix is the previous code to generate an image using bitmap with specific width and height.

How I can Make this function work by setting automatic width and height as the given html code, because some times the given html is more than the given dimensional?

Thanks in advance.

Firas Msw
  • 27
  • 6
  • 1
    [WebBrowser Html Document to Image](https://stackoverflow.com/a/60741246/7444103) – Jimi Jan 14 '21 at 11:22
  • @Jimi I'm not using a url web, I am using a dummy html to generate an inage using bitmap – Firas Msw Jan 14 '21 at 12:36
  • 1
    You're using a WebBrowser Control to render HTML content. That's all that matters. Read the notes in that post. – Jimi Jan 14 '21 at 12:38

0 Answers0