Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. LoadCompletedEventHandler h = null;
  2. h = (o, e) =>
  3. {
  4. WpfControlRenderer.CreateControlScreenshot((FrameworkElement)o, destFileName);
  5. _wb.LoadCompleted -= h;
  6. };
  7. _wb.LoadCompleted += h;
  8. var size = new Size(_wb.Width, _wb.Height);
  9. _wb.Measure(size);
  10. _wb.Arrange(new Rect(size));
  11. _wb.Navigate(url);
  12.  
  13. class NativeMethods
  14. {
  15. [ComImport]
  16. [Guid("0000010D-0000-0000-C000-000000000046")]
  17. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  18. interface IViewObject
  19. {
  20. 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);
  21. }
  22.  
  23. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  24. struct RECT
  25. {
  26. public int Left;
  27. public int Top;
  28. public int Right;
  29. public int Bottom;
  30. }
  31.  
  32. public static void GetImage(object obj, Image destination, Color backgroundColor)
  33. {
  34. using(Graphics graphics = Graphics.FromImage(destination))
  35. {
  36. IntPtr deviceContextHandle = IntPtr.Zero;
  37. RECT rectangle = new RECT();
  38.  
  39. rectangle.Right = destination.Width;
  40. rectangle.Bottom = destination.Height;
  41.  
  42. graphics.Clear(backgroundColor);
  43.  
  44. try
  45. {
  46. deviceContextHandle = graphics.GetHdc();
  47.  
  48. IViewObject viewObject = obj as IViewObject;
  49. viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0);
  50. }
  51. finally
  52. {
  53. if(deviceContextHandle != IntPtr.Zero)
  54. {
  55. graphics.ReleaseHdc(deviceContextHandle);
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. Bitmap screenshot = new Bitmap(1024, 768);
  63. NativeMethods.GetImage(webBrowser.ActiveXInstance, screenshot, Color.White);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement