• 使用webBrowse把网页word等转换成图片


    http://blog.csdn.net/zgke/article/details/3248497

    使用

    1. Bitmap MyImage = Test.GetControlScrollImage(new Uri(@"http://www.sina.com.cn"), 1024);
    2. MyImage.Save(@"C:/1.BMP");
    3. MyImage.Dispose();

    使用到的类

     

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Text;
    7. using System.Windows.Forms;
    8. using System.Drawing.Imaging;
    9. using System.Runtime.InteropServices;
    10. using System.Security;
    11. namespace Print
    12. {
    13.     public class Test
    14.     {
    15.         public static Bitmap GetHtmlImage(Uri UrlString,int Width)
    16.         {
    17.             WebBrowser MyControl = new WebBrowser();
    18.             MyControl.Size = new Size(Width, 10);
    19.             MyControl.Url = UrlString;
    20.             while (MyControl.ReadyState != WebBrowserReadyState.Complete)
    21.             {
    22.                 Application.DoEvents();
    23.             }
    24.            
    25.           
    26.             MyControl.Height= MyControl.Document.Body.ScrollRectangle.Height+20;
    27.             MyControl.Url = UrlString;
    28.             WebControlImage.Snapshot snap = new WebControlImage.Snapshot();
    29.             Bitmap MyImage= snap.TakeSnapshot(MyControl.ActiveXInstance, new Rectangle(0, 0, MyControl.Width, MyControl.Height));
    30.             MyControl.Dispose();
    31.             return MyImage;
    32.             
    33.         }      
    34.         /// <summary>
    35.         /// WebBrowser获取图形
    36.         /// </summary>
    37.         private class WebControlImage
    38.         {
    39.             internal static class NativeMethods
    40.             {
    41.                 [StructLayout(LayoutKind.Sequential)]
    42.                 public sealed class tagDVTARGETDEVICE
    43.                 {
    44.                     [MarshalAs(UnmanagedType.U4)]
    45.                     public int tdSize;
    46.                     [MarshalAs(UnmanagedType.U2)]
    47.                     public short tdDriverNameOffset;
    48.                     [MarshalAs(UnmanagedType.U2)]
    49.                     public short tdDeviceNameOffset;
    50.                     [MarshalAs(UnmanagedType.U2)]
    51.                     public short tdPortNameOffset;
    52.                     [MarshalAs(UnmanagedType.U2)]
    53.                     public short tdExtDevmodeOffset;
    54.                 }
    55.                 [StructLayout(LayoutKind.Sequential)]
    56.                 public class COMRECT
    57.                 {
    58.                     public int left;
    59.                     public int top;
    60.                     public int right;
    61.                     public int bottom;
    62.                     public COMRECT()
    63.                     {
    64.                     }
    65.                     public COMRECT(Rectangle r)
    66.                     {
    67.                         this.left = r.X;
    68.                         this.top = r.Y;
    69.                         this.right = r.Right;
    70.                         this.bottom = r.Bottom;
    71.                     }
    72.                     public COMRECT(int left, int top, int right, int bottom)
    73.                     {
    74.                         this.left = left;
    75.                         this.top = top;
    76.                         this.right = right;
    77.                         this.bottom = bottom;
    78.                     }
    79.                     public static NativeMethods.COMRECT FromXYWH(int x, int y, int width, int height)
    80.                     {
    81.                         return new NativeMethods.COMRECT(x, y, x + width, y + height);
    82.                     }
    83.                     public override string ToString()
    84.                     {
    85.                         return string.Concat(new object[] { "Left = "this.left, " Top "this.top, " Right = "this.right, " Bottom = "this.bottom });
    86.                     }
    87.                 }
    88.                 [StructLayout(LayoutKind.Sequential)]
    89.                 public sealed class tagLOGPALETTE
    90.                 {
    91.                     [MarshalAs(UnmanagedType.U2)]
    92.                     public short palVersion;
    93.                     [MarshalAs(UnmanagedType.U2)]
    94.                     public short palNumEntries;
    95.                 }
    96.             }
    97.             public class Snapshot
    98.             {
    99.                 /// <summary>
    100.                 /// 取快照
    101.                 /// </summary>
    102.                 /// <param name="pUnknown">Com 对象</param>
    103.                 /// <param name="bmpRect">图象大小</param>
    104.                 /// <returns></returns>
    105.                 public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect)
    106.                 {
    107.                     if (pUnknown == null)
    108.                         return null;
    109.                     //必须为com对象
    110.                     if (!Marshal.IsComObject(pUnknown))
    111.                         return null;
    112.                     //IViewObject 接口
    113.                     UnsafeNativeMethods.IViewObject ViewObject = null;
    114.                     IntPtr pViewObject = IntPtr.Zero;
    115.                     //内存图
    116.                     Bitmap pPicture = new Bitmap(bmpRect.Width, bmpRect.Height);
    117.                     Graphics hDrawDC = Graphics.FromImage(pPicture);
    118.                     //获取接口
    119.                     object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown),
    120.                         ref UnsafeNativeMethods.IID_IViewObject, out pViewObject);
    121.                     try
    122.                     {
    123.                         ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject;
    124.                         //调用Draw方法
    125.                         ViewObject.Draw((int)System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT,
    126.                             -1,
    127.                             IntPtr.Zero,
    128.                             null,
    129.                             IntPtr.Zero,
    130.                             hDrawDC.GetHdc(),
    131.                             new NativeMethods.COMRECT(bmpRect),
    132.                             null,
    133.                             IntPtr.Zero,
    134.                             0);
    135.                     }
    136.                     catch (Exception ex)
    137.                     {
    138.                         Console.WriteLine(ex.Message);
    139.                         throw ex;
    140.                     }
    141.                     //释放
    142.                     hDrawDC.Dispose();
    143.                     return pPicture;
    144.                 }
    145.             }
    146.             [SuppressUnmanagedCodeSecurity]
    147.             internal static class UnsafeNativeMethods
    148.             {
    149.                 public static Guid IID_IViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}");
    150.                 [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    151.                 public interface IViewObject
    152.                 {
    153.                     [PreserveSig]
    154.                     int Draw([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [In] NativeMethods.COMRECT lprcBounds, [In] NativeMethods.COMRECT lprcWBounds, IntPtr pfnContinue, [In] int dwContinue);
    155.                     [PreserveSig]
    156.                     int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hicTargetDev, [Out] NativeMethods.tagLOGPALETTE ppColorSet);
    157.                     [PreserveSig]
    158.                     int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);
    159.                     [PreserveSig]
    160.                     int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze);
    161.                     void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IAdviseSink pAdvSink);
    162.                     void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, [In, Out, MarshalAs(UnmanagedType.LPArray)] System.Runtime.InteropServices.ComTypes.IAdviseSink[] pAdvSink);
    163.                 }
    164.             }
    165.         }
    166.     }
    167. }
  • 相关阅读:
    Android移动view动画问题
    GIT常用操作
    linux下mysql安装
    jdk安装
    linux下Tomcat安装
    猜测性能瓶颈
    MySQL没有远程连接权限设置
    linux下jmeter使用帮助
    BI的核心价值[转]
    BI与大数据
  • 原文地址:https://www.cnblogs.com/mingyongcheng/p/3492710.html
Copyright © 2020-2023  润新知