国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Winform將網頁生成圖片

2019-11-17 01:45:09
字體:
來源:轉載
供稿:網友

Winform將網頁生成圖片

今天無意見看到瀏覽器有將網頁生成圖片的功能,頓時趕腳很好奇,于是就找了找資料自己做了一個類似的功能。

工具截圖:生成后的圖片

手動填寫網站地址,可選擇圖片類型和保持圖片地址,來生成頁面的圖片,當圖片路徑未選擇時則保存桌面;

具體代碼如下:

將html生成圖片的類

  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             MyControl.Height = MyControl.Document.Body.ScrollRectangle.Height + 20; 25             MyControl.Url = UrlString; 26             WebControlImage.Snapshot snap = new WebControlImage.Snapshot(); 27             Bitmap MyImage = snap.TakeSnapshot(MyControl.ActiveXInstance, new Rectangle(0, 0, MyControl.Width, MyControl.Height)); 28             MyControl.Dispose(); 29             return MyImage; 30         } 31         ///  32         /// WebBrowser獲取圖形  33         ///  34         private class WebControlImage 35         { 36             internal static class NativeMethods 37             { 38                 [StructLayout(LayoutKind.Sequential)] 39                 public sealed class tagDVTARGETDEVICE 40                 { 41                     [MarshalAs(UnmanagedType.U4)] 42                     public int tdSize; 43                     [MarshalAs(UnmanagedType.U2)] 44                     public short tdDriverNameOffset; 45                     [MarshalAs(UnmanagedType.U2)] 46                     public short tdDeviceNameOffset; 47                     [MarshalAs(UnmanagedType.U2)] 48                     public short tdPortNameOffset; 49                     [MarshalAs(UnmanagedType.U2)] 50                     public short tdExtDevmodeOffset; 51                 } 52                 [StructLayout(LayoutKind.Sequential)] 53                 public class COMRECT 54                 { 55                     public int left; 56                     public int top; 57                     public int right; 58                     public int bottom; 59                     public COMRECT() 60                     { 61                     } 62                     public COMRECT(Rectangle r) 63                     { 64                         this.left = r.X; 65                         this.top = r.Y; 66                         this.right = r.Right; 67                         this.bottom = r.Bottom; 68                     } 69                     public COMRECT(int left, int top, int right, int bottom) 70                     { 71                         this.left = left; 72                         this.top = top; 73                         this.right = right; 74                         this.bottom = bottom; 75                     } 76                     public static NativeMethods.COMRECT FromXYWH(int x, int y, int width, int height) 77                     { 78                         return new NativeMethods.COMRECT(x, y, x + width, y + height); 79                     } 80                     public override string ToString() 81                     { 82                         return string.Concat(new object[] { "Left = ", this.left, " Top ", this.top, " Right = ", this.right, " Bottom = ", this.bottom }); 83                     } 84                 } 85                 [StructLayout(LayoutKind.Sequential)] 86                 public sealed class tagLOGPALETTE 87                 { 88                     [MarshalAs(UnmanagedType.U2)] 89                     public short palVersion; 90                     [MarshalAs(UnmanagedType.U2)] 91                     public short palNumEntries; 92                 } 93             } 94             public class Snapshot 95             { 96                 ///  97                 /// ?煺?  98                 ///  99                 /// Com 對象 100                 /// 圖象大小 101                 /// 102                 public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect)103                 {104                     if (pUnknown == null)105                         return null;106                     //必須為com對象 107                     if (!Marshal.IsComObject(pUnknown))108                         return null;109                     //IViewObject 接口 110                     UnsafeNativeMethods.IViewObject ViewObject = null;111                     IntPtr pViewObject = IntPtr.Zero;112                     //內存圖 113                     Bitmap pPicture = new Bitmap(bmpRect.Width, bmpRect.Height);114                     Graphics hDrawDC = Graphics.FromImage(pPicture);115                     //獲取接口 116                     object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown),117                     ref UnsafeNativeMethods.IID_IViewObject, out pViewObject);118                     try119                     {120                         ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject;121                         //調用Draw方法 122                         ViewObject.Draw((int)System.Runtime.InteropServices.ComTypes.DVaspECT.DVASPECT_CONTENT,123                         -1,124                         IntPtr.Zero,125                         null,126                         IntPtr.Zero,127                         hDrawDC.GetHdc(),128                         new NativeMethods.COMRECT(bmpRect),129                         null,130                         IntPtr.Zero,131                         0);132                     }133                     catch (Exception ex)134                     {135                         Console.WriteLine(ex.Message);136                         throw ex;137                     }138                     //釋放 139                     hDrawDC.Dispose();140                     return pPicture;141                 }142             }143             [SuppressUnmanagedCodeSecurity]144             internal static class UnsafeNativeMethods145             {146                 public static Guid IID_IViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}");147                 [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]148                 public interface IViewObject149                 {150                     [PreserveSig]151                     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);152                     [PreserveSig]153                     int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hicTargetDev, [Out] NativeMethods.tagLOGPALETTE ppColorSet);154                     [PreserveSig]155                     int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);156                     [PreserveSig]157                     int Unfreeze
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌| 普安县| 福州市| 凤山县| 沈阳市| 惠来县| 九龙县| 来安县| 平远县| 屏东市| 安仁县| 镇原县| 德令哈市| 磐石市| 定远县| 达日县| 纳雍县| 襄汾县| 花莲县| 铁岭县| 成都市| 繁峙县| 咸阳市| 招远市| 临洮县| 搜索| 安徽省| 阳西县| 永顺县| 巴塘县| 宁南县| 丰顺县| 吴江市| 正镶白旗| 安远县| 芷江| 通州区| 葫芦岛市| 北宁市| 绥滨县| 南平市|