Welcome Guest Search | Active Topics | Sign In | Register

Capture a WebView Options
Francesco Marra
Posted: Sunday, May 31, 2020 10:06:38 AM
Rank: Newbie
Groups: Member

Joined: 5/3/2019
Posts: 7
Hi, I would like to take a screenshot of a page. My main goal is to send this screenshot in order to print it with "goolge chrome print".
The page that I have to print use some parameter in the php session (I can't send the url to chrome) and it generates a ticket.

Here the script:

Code: C#
public void newwindowEvt(object sender, NewWindowEventArgs e)
        {
            string url = e.TargetUrl.ToString();
            if (!checkMaxTab(url)) return; 

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {

                WebControl wctr= new WebControl();
                EO.Wpf.TabItem newtab = new EO.Wpf.TabItem();

                e.WebView.CertificateError += webview_CertificateError;
                e.WebView.BeforeContextMenu += new BeforeContextMenuHandler(WebView_BeforeContextMenu);
                e.WebView.BeforePrint += new BeforePrintHandler(printhand);

                newtab.Content = wctr;
                newtab.Header = url;
                

                if (!url_txt.IsFocused)
                {
                    url_txt.Text = url;
                }

              
                wctr.WebView = e.WebView;
                wcSelected = wctr;

                myWV.Add(wctr);
                
                tabCtrl.Items.Add(newtab);

                tabCtrl.SelectedItem = newtab;
               

            }));


            e.Accepted = true;

        }


        void screenshot()
        {

            EO.WebBrowser.WebView wb=null;

            try
            {

                wb = wcSelected.WebView;
                wb.ZoomFactor = 0.6f;  //this in order to get the whole page

               
                System.Drawing.Image img=null;      
                System.Drawing.Size s = wb.GetPageSize();

               
                System.Threading.Thread.Sleep(200);
                wb = wcSelected.WebView;
                
                img = wb.Capture();
                s = wb.GetPageSize();
                
                System.Drawing.Bitmap bmSnapshot = new System.Drawing.Bitmap(img);
                
                bmSnapshot.Save(pathPrintFile, ImageFormat.Png);
                
                printChrome(pathPrintFile);

            }
            catch (Exception e) {

                MessageBox.Show(e.Message);

            }
         
            wb.ZoomFactor = mylocalConfiguration.zoom/100;

        }



The screenshot function is linked to a button. It works with every page but it doesn't if the page comes from a newwindowEvt event.
The error says:
"reference to an object not set on an object instance" on this line "img = wb.Capture();"
eo_support
Posted: Monday, June 1, 2020 1:45:08 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

Your code looks fine to us. You can check the stack trace of the error first. If the stack trace ends inside Capture in our code, then you can try to isolate the problem into a test project and send the test project to us:

https://www.essentialobjects.com/forum/test_project.aspx

If the stack trace does not go into Capture, then issue is because variable "wb" is null. In that case you will need to troubleshoot your own code to find out why wb is null.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.