Printing from System.Windows.Forms.WebBrowser RRS feed

  • Question

  • Hi,

       I have a windows form (not WPF) application with a System.Windows.Forms.WebBrowser control. I have text marked up in  the control with background highlighting, underlining and borders for specific characters (see below for image). I would like users to be able to print and retain all the formatting. I tried using the browser's print with RightClick-Print. That does not print the highlight colors. I tried copying and pasting into Word. That does not print the borders. So then I cobbled together the following code (which I don't quite understand):

     private void printBowserButton_Click(object sender, EventArgs e)
                System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
                printDoc.PrintPage += PrintDoc_PrintPage;
            private void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
                Bitmap image = TakeScreenShot(startStopRegionBrowser);
                e.Graphics.DrawImage(image, e.MarginBounds.Left, e.MarginBounds.Top);
            private Bitmap TakeScreenShot(Control control)
                Bitmap tmpImg = new Bitmap(control.Width, control.Height);
                using (Graphics g = Graphics.FromImage(tmpImg))
                        control.PointToScreen(new Point(0, 0)),
                        new Point(0, 0),
                        new Size(control.Width, control.Height));
                return tmpImg;

    This works and prints an image of the portion of the webbrowser control that is visible when the print button is clicked. I would really like to print the complete document. Any suggestions?

    I am programming in C# using Visual Studio Enterprise 2017 with .Net Framework 4.6.1. I am running Windows 10. My default Web Browser is IE 11 (does that matter?).

    If this is the wrong forum, can you please point me in the right direction?



    Ethan Strauss

    Tuesday, January 22, 2019 6:17 PM


All replies