1 /// <summary> 2 /// 图片类型枚举 3 /// </summary> 4 public enum ImageType 5 { 6 GIF = 0, 7 JPG = 1, 8 PNG = 2 9 } 10 11 /// <summary> 12 /// 图片辅助类 13 /// </summary> 14 public class ImageHelper 15 { 16 /// <summary> 17 /// 将Url地址保存为图片 18 /// </summary> 19 /// <param name="url">网页路径</param> 20 /// <param name="savePath">图片存放路径</param> 21 /// <param name="imageType">图片类型</param> 22 /// <returns></returns> 23 public static string SaveUrlToImage(string url, string savePath, ImageType imageType) 24 { 25 var uri = new Uri(url); 26 using (var bit = GetHtmlImage(uri, Screen.PrimaryScreen.Bounds.Width)) 27 { 28 switch (imageType) 29 { 30 case ImageType.GIF: 31 bit.Save(savePath, ImageFormat.Gif); 32 break; 33 case ImageType.JPG: 34 bit.Save(savePath, ImageFormat.Jpeg); 35 break; 36 case ImageType.PNG: 37 bit.Save(savePath, ImageFormat.Png); 38 break; 39 default: 40 bit.Save(savePath, ImageFormat.Jpeg); 41 break; 42 } 43 return savePath; 44 } 45 } 46 47 protected static Bitmap GetHtmlImage(Uri urlString, int width) 48 { 49 using (var control = new WebBrowser { Size = new Size(width, 10), Url = urlString, ScriptErrorsSuppressed = true }) 50 { 51 while (control.ReadyState != WebBrowserReadyState.Complete) 52 { 53 Application.DoEvents(); 54 } 55 if (control.Document != null) 56 { 57 if (control.Document.Body != null) 58 { 59 control.Height = control.Document.Body.ScrollRectangle.Height + 20; 60 } 61 } 62 control.Url = urlString; 63 var snap = new WebControlImage.Snapshot(); 64 var bitmap = snap.TakeSnapshot(control.ActiveXInstance, new Rectangle(0, 0, control.Width, control.Height)); 65 control.Dispose(); 66 return bitmap; 67 } 68 } 69 70 /// WebBrowser获取图形 71 protected class WebControlImage 72 { 73 internal static class NativeMethods 74 { 75 [StructLayout(LayoutKind.Sequential)] 76 public sealed class TagDvtargetdevice 77 { 78 [MarshalAs(UnmanagedType.U4)] 79 public int tdSize; 80 [MarshalAs(UnmanagedType.U2)] 81 public short tdDriverNameOffset; 82 [MarshalAs(UnmanagedType.U2)] 83 public short tdDeviceNameOffset; 84 [MarshalAs(UnmanagedType.U2)] 85 public short tdPortNameOffset; 86 [MarshalAs(UnmanagedType.U2)] 87 public short tdExtDevmodeOffset; 88 } 89 90 [StructLayout(LayoutKind.Sequential)] 91 public class Comrect 92 { 93 public int left; 94 public int top; 95 public int right; 96 public int bottom; 97 98 public Comrect() 99 { 100 } 101 102 public Comrect(Rectangle r) 103 { 104 left = r.X; 105 top = r.Y; 106 right = r.Right; 107 bottom = r.Bottom; 108 } 109 110 public Comrect(int left, int top, int right, int bottom) 111 { 112 this.left = left; 113 this.top = top; 114 this.right = right; 115 this.bottom = bottom; 116 } 117 118 public static Comrect FromXywh(int x, int y, int width, int height) 119 { 120 return new Comrect(x, y, x + width, y + height); 121 } 122 123 public override string ToString() 124 { 125 return string.Concat("Left = ", left, " Top ", top, " Right = ", right, " Bottom = ", bottom); 126 } 127 } 128 129 [StructLayout(LayoutKind.Sequential)] 130 public sealed class TagLogpalette 131 { 132 [MarshalAs(UnmanagedType.U2)] 133 public short palVersion; 134 [MarshalAs(UnmanagedType.U2)] 135 public short palNumEntries; 136 } 137 } 138 139 public class Snapshot 140 { 141 /// 图象大小 142 public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect) 143 { 144 if (pUnknown == null) { return null; } 145 //必须为com对象 146 if (!Marshal.IsComObject(pUnknown)) { return null; } 147 //IViewObject 接口 148 IntPtr viewObject; 149 //内存图 150 var bitmap = new Bitmap(bmpRect.Width, bmpRect.Height); 151 var image = Graphics.FromImage(bitmap); 152 //获取接口 153 object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown), ref UnsafeNativeMethods.IidIViewObject, out viewObject); 154 try 155 { 156 var o = (UnsafeNativeMethods.IViewObject)Marshal.GetTypedObjectForIUnknown(viewObject, typeof(UnsafeNativeMethods.IViewObject)); 157 //调用Draw方法 158 if (o != null) 159 { 160 o.Draw((int)DVASPECT.DVASPECT_CONTENT, -1, IntPtr.Zero, null, IntPtr.Zero, image.GetHdc(), new NativeMethods.Comrect(bmpRect), null, IntPtr.Zero, 0); 161 } 162 } 163 catch (Exception ex) 164 { 165 Console.WriteLine(ex.Message); 166 throw; 167 } 168 //释放 169 image.Dispose(); 170 return bitmap; 171 } 172 } 173 174 [SuppressUnmanagedCodeSecurity] 175 internal static class UnsafeNativeMethods 176 { 177 public static Guid IidIViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}"); 178 179 [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), 180 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 181 public interface IViewObject 182 { 183 [PreserveSig] 184 int Draw([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, 185 [In] NativeMethods.TagDvtargetdevice ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, 186 [In] NativeMethods.Comrect lprcBounds, [In] NativeMethods.Comrect lprcWBounds, 187 IntPtr pfnContinue, 188 [In] int dwContinue); 189 190 [PreserveSig] 191 int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, 192 [In] NativeMethods.TagDvtargetdevice ptd, IntPtr hicTargetDev, 193 [Out] NativeMethods.TagLogpalette ppColorSet); 194 195 [PreserveSig] 196 int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, 197 [Out] IntPtr pdwFreeze); 198 199 [PreserveSig] 200 int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze); 201 202 void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, 203 [In, MarshalAs(UnmanagedType.U4)] int advf, 204 [In, MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink); 205 206 void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, 207 [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, 208 [In, Out, MarshalAs(UnmanagedType.LPArray)] IAdviseSink[] pAdvSink); 209 } 210 } 211 } 212 }