Here I asked to create an image of the control about the screenshot of the control, the issue is resolved, but with a browser ( WebBrowser ) it does not roll. Browser Fota need a little different, but not the problem. When I create a browser in code, it does not go to pages.

 LoadCompletedEventHandler h = null; h = (o, e) => { WpfControlRenderer.CreateControlScreenshot((FrameworkElement)o, destFileName); _wb.LoadCompleted -= h; }; _wb.LoadCompleted += h; var size = new Size(_wb.Width, _wb.Height); _wb.Measure(size); _wb.Arrange(new Rect(size)); _wb.Navigate(url); 

The function h does not even enter. At the same time, the browser added to the form perfectly opens everything and fotkat. He lacks something, but I do not know what.

UPD:

The WebBrowser.Navigating and WebBrowser.Navigated events are triggered. But WebBrowser.LoadCompleted for some reason does not occur

  • And tell me, do you need a WebBrowser control? Or just a screenshot of a specific site? - Vadim Ovchinnikov
  • Well, the content in WebBrowser is not control :) LoadCompleted does not work, because WPF is not an idiot to load content and render an extra load on the render. - Sublihim
  • @Sublihim, thanks, you really helped me now))) - iRumba
  • @VadimOvchinnikov screenshot is needed. It is desirable with a minimum load, because the screenshots will require several thousand. - iRumba
  • @iRumba Look, I can offer you this solution: A clean console without rendering a browser control and screenshots. Just enter the URL - get a screenshot. You can also pre-execute any JavaScript (modify something on the page), if necessary. Does this suit you? - Vadim Ovchinnikov

1 answer 1

I don’t know if it helps, but there is a response to stackoverflow "Converting WebBrowser.Document To A Bitmap?" .
The correct answer to the question is given by the user webspy .

Here is an example of its code:

 class NativeMethods { [ComImport] [Guid("0000010D-0000-0000-C000-000000000046")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IViewObject { void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue); } [StructLayout(LayoutKind.Sequential, Pack = 4)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } public static void GetImage(object obj, Image destination, Color backgroundColor) { using(Graphics graphics = Graphics.FromImage(destination)) { IntPtr deviceContextHandle = IntPtr.Zero; RECT rectangle = new RECT(); rectangle.Right = destination.Width; rectangle.Bottom = destination.Height; graphics.Clear(backgroundColor); try { deviceContextHandle = graphics.GetHdc(); IViewObject viewObject = obj as IViewObject; viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0); } finally { if(deviceContextHandle != IntPtr.Zero) { graphics.ReleaseHdc(deviceContextHandle); } } } } } 

Example of use:

 Bitmap screenshot = new Bitmap(1024, 768); NativeMethods.GetImage(webBrowser.ActiveXInstance, screenshot, Color.White); 

The only "but" , this example uses a browser from WinForms . But no one forbids you to create an instance of this class in memory and get a screenshot.

  • Not bad. If you yourself figured out what is written here, can you tell me another way to find out the height of the loaded page in order to make a full-height screenshot? - iRumba
  • @iRumba. Most likely, you need to request the size of the downloaded content from the browser itself webBrowser.Document.Body.ScrollRectangle.Height , respectively, it seems and should be set as the height of the bitmap - Sublihim
  • @iRumba, if that doesn't work, then there is a more grander way to calculate the height and width of the content. I can add in response, if necessary. True, there one moment confuses me how an invisible browser will react to a change in its height and width. - Sublihim