zoukankan      html  css  js  c++  java
  • C#实现网页截图功能

    1. //需要添加System.Drawing及System.Windows.Forms引用   
    2. using System;   
    3. using System.Drawing;   
    4. using System.Drawing.Drawing2D;   
    5. using System.Drawing.Imaging;   
    6. using System.Windows.Forms;   
    7.   
    8.   
    9. namespace 网页截图   
    10. {   
    11.     class Program   
    12.     {   
    13.         [STAThread]   
    14.         static void Main(string[] args)   
    15.         {   
    16.             string url = "http://www.yongfa365.com/";   
    17.             MyLib.GetImage thumb = new MyLib.GetImage(url, 1024, 4000, 1024, 4000);   
    18.             System.Drawing.Bitmap x = thumb.GetBitmap();   
    19.             string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");   
    20.   
    21.             x.Save(@"C:\" + FileName + ".jpg");   
    22.             Console.WriteLine("成功");   
    23.             Console.ReadKey();   
    24.         }   
    25.     }   
    26. }   
    27.   
    28.   
    29.   
    30.   
    31.   
    32. namespace MyLib   
    33. {   
    34.     public class GetImage   
    35.     {   
    36.         private int S_Height;   
    37.         private int S_Width;   
    38.         private int F_Height;   
    39.         private int F_Width;   
    40.         private string MyURL;   
    41.   
    42.         public int ScreenHeight   
    43.         {   
    44.             get { return S_Height; }   
    45.             set { S_Height = value; }   
    46.         }   
    47.   
    48.         public int ScreenWidth   
    49.         {   
    50.             get { return S_Width; }   
    51.             set { S_Width = value; }   
    52.         }   
    53.   
    54.         public int ImageHeight   
    55.         {   
    56.             get { return F_Height; }   
    57.             set { F_Height = value; }   
    58.         }   
    59.   
    60.         public int ImageWidth   
    61.         {   
    62.             get { return F_Width; }   
    63.             set { F_Width = value; }   
    64.         }   
    65.   
    66.         public string WebSite   
    67.         {   
    68.             get { return MyURL; }   
    69.             set { MyURL = value; }   
    70.         }   
    71.   
    72.         public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)   
    73.         {   
    74.             this.WebSite = WebSite;   
    75.             this.ScreenWidth = ScreenWidth;   
    76.             this.ScreenHeight = ScreenHeight;   
    77.             this.ImageHeight = ImageHeight;   
    78.             this.ImageWidth = ImageWidth;   
    79.         }   
    80.   
    81.         public Bitmap GetBitmap()   
    82.         {   
    83.             WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);   
    84.             Shot.GetIt();   
    85.             Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);   
    86.             return Pic;   
    87.         }   
    88.     }   
    89.   
    90.     class WebPageBitmap   
    91.     {   
    92.         WebBrowser MyBrowser;   
    93.         string URL;   
    94.         int Height;   
    95.         int Width;   
    96.   
    97.         public WebPageBitmap(string url, int width, int height)   
    98.         {   
    99.             this.Height = height;   
    100.             this.Width = width;   
    101.             this.URL = url;   
    102.             MyBrowser = new WebBrowser();   
    103.             MyBrowser.ScrollBarsEnabled = false;   
    104.             MyBrowser.Size = new Size(this.Width, this.Height);   
    105.         }   
    106.   
    107.         public void GetIt()   
    108.         {   
    109.             MyBrowser.Navigate(this.URL);   
    110.             while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)   
    111.             {   
    112.                 Application.DoEvents();   
    113.             }   
    114.         }   
    115.   
    116.         public Bitmap DrawBitmap(int theight, int twidth)   
    117.         {   
    118.             Bitmap myBitmap = new Bitmap(Width, Height);   
    119.             Rectangle DrawRect = new Rectangle(0, 0, Width, Height);   
    120.             MyBrowser.DrawToBitmap(myBitmap, DrawRect);   
    121.             System.Drawing.Image imgOutput = myBitmap;   
    122.             System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);   
    123.             Graphics g = Graphics.FromImage(oThumbNail);   
    124.             g.CompositingQuality = CompositingQuality.HighSpeed;   
    125.             g.SmoothingMode = SmoothingMode.HighSpeed;   
    126.             g.InterpolationMode = InterpolationMode.HighQualityBilinear;   
    127.             Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);   
    128.             g.DrawImage(imgOutput, oRectangle);   
    129.             try  
    130.             {   
    131.                 return (Bitmap)oThumbNail;   
    132.             }   
    133.             catch (Exception ex)   
    134.             {   
    135.                 return null;   
    136.             }   
    137.             finally  
    138.             {   
    139.                 imgOutput.Dispose();   
    140.                 imgOutput = null;   
    141.                 MyBrowser.Dispose();   
    142.                 MyBrowser = null;   
    143.             }   
    144.         }   
    145.     }   
    146.   
    147. }  
  • 相关阅读:
    Surface Mount Package Details
    Boost Converter
    IPC low/medium/high density 什么意思?
    SMT Surface Mount Technology footprint references
    Time Step Too Small in Multisim
    mOByDiC E90C2600 EOBD/OBDII to RS232 gateway
    STN1110 Multiprotocol OBD to UART Interpreter
    STN1170 Multiprotocol OBD to UART Interpreter
    BR16F84 OBD II Interface Chip For PWM, VPW, and ISO 9141-2 Vehicles
    ELM327 OBD to RS232 Interpreters
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1762657.html
Copyright © 2011-2022 走看看