C#實現網頁截圖功能
//需要新增System.Drawing及System.Windows.Forms引用
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace 網頁截圖
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string url = "http://www.yongfa365.com/";
MyLib.GetImage thumb = new MyLib.GetImage(url, 1024, 4000, 1024, 4000);
System.Drawing.Bitmap x = thumb.GetBitmap();
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(@"C:\" + FileName + ".jpg");
Console.WriteLine("成功");
Console.ReadKey();
}
}
}
namespace MyLib
{
public class GetImage
{
private int S_Height;
private int S_Width;
private int F_Height;
private int F_Width;
private string MyURL;
public int ScreenHeight
{
get { return S_Height; }
set { S_Height = value; }
}
public int ScreenWidth
{
get { return S_Width; }
set { S_Width = value; }
}
public int ImageHeight
{
get { return F_Height; }
set { F_Height = value; }
}
public int ImageWidth
{
get { return F_Width; }
set { F_Width = value; }
}
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}
public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
{
this.WebSite = WebSite;
this.ScreenWidth = ScreenWidth;
this.ScreenHeight = ScreenHeight;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
}
class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url, int width, int height)
{
this.Height = height;
this.Width = width;
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
MyBrowser.Size = new Size(this.Width, this.Height);
}
public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
public Bitmap DrawBitmap(int theight, int twidth)
{
Bitmap myBitmap = new Bitmap(Width, Height);
Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
System.Drawing.Image ThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle Rectangle = new Rectangle(0, 0, twidth, theight);
g.DrawImage(imgOutput, oRectangle);
try
{
return (Bitmap)oThumbNail;
}
catch (Exception ex)
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
}
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace 網頁截圖
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string url = "http://www.yongfa365.com/";
MyLib.GetImage thumb = new MyLib.GetImage(url, 1024, 4000, 1024, 4000);
System.Drawing.Bitmap x = thumb.GetBitmap();
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(@"C:\" + FileName + ".jpg");
Console.WriteLine("成功");
Console.ReadKey();
}
}
}
namespace MyLib
{
public class GetImage
{
private int S_Height;
private int S_Width;
private int F_Height;
private int F_Width;
private string MyURL;
public int ScreenHeight
{
get { return S_Height; }
set { S_Height = value; }
}
public int ScreenWidth
{
get { return S_Width; }
set { S_Width = value; }
}
public int ImageHeight
{
get { return F_Height; }
set { F_Height = value; }
}
public int ImageWidth
{
get { return F_Width; }
set { F_Width = value; }
}
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}
public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
{
this.WebSite = WebSite;
this.ScreenWidth = ScreenWidth;
this.ScreenHeight = ScreenHeight;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Shot.GetIt();
Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}
}
class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url, int width, int height)
{
this.Height = height;
this.Width = width;
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
MyBrowser.Size = new Size(this.Width, this.Height);
}
public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
public Bitmap DrawBitmap(int theight, int twidth)
{
Bitmap myBitmap = new Bitmap(Width, Height);
Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
System.Drawing.Image ThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle Rectangle = new Rectangle(0, 0, twidth, theight);
g.DrawImage(imgOutput, oRectangle);
try
{
return (Bitmap)oThumbNail;
}
catch (Exception ex)
{
return null;
}
finally
{
imgOutput.Dispose();
imgOutput = null;
MyBrowser.Dispose();
MyBrowser = null;
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-620521/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#實現截圖功能C#
- Python網頁截圖/螢幕截圖/截長圖如何實現?Python網頁
- PHP如何實現網頁截圖?PHP網頁
- canvas實現截圖功能Canvas
- javascript實現網頁截圖匯出方案JavaScript網頁
- golang快速實現服務端網頁截圖Golang服務端網頁
- 支付寶截圖反饋功能實現
- 如何在Mac網頁中長截圖?蘋果電腦網頁截圖怎麼截長圖?Mac網頁蘋果
- html2canvas-實現頁面截圖HTMLCanvas
- Python+Selenium+phantomjs實現網頁模擬登入和截圖PythonJS網頁
- 使用chromedriver抓取網頁截圖Chrome網頁
- 網頁截圖工具:WebShot for Mac網頁WebMac
- 免費的網頁截圖API網頁API
- 怎麼生成完整的網頁截圖? Full Page Screen Capture 幫你實現網頁APT
- js實現視訊截圖,視訊批量截圖,canvas實現JSCanvas
- js實現網頁端錄音功能JS網頁
- 實現Google帶截圖功能的web反饋外掛GoWeb
- chrome-網頁gif截圖外掛Chrome網頁
- 網頁完整的長截圖怎麼截?3步搞定!網頁
- C# 實現PPT 每一頁轉成圖片C#
- phantomjs實現免費線上網頁截圖工具-toolfk程式設計師線上工具網JS網頁程式設計師
- web端 網頁端分享功能的實現Web網頁
- 原生JS實現影片截圖JS
- Selenium IDE 如何實現截圖IDE
- Android長截圖的實現Android
- selenium實現螢幕截圖
- 織夢點選圖片實現下一頁功能
- Mybatis 分頁:Pagehelper + 攔截器實現MyBatis
- 分頁功能的實現
- 《FireShot》一鍵滾動截圖整個網頁網頁
- 微信長按網頁儲存為截圖網頁
- 從0到1,手把手帶你開發截圖工具ScreenCap------001實現基本的截圖功能
- 網頁佈局------輪播圖效果實現網頁
- selenium提供的截圖功能
- python 三種方式實現截圖Python
- WPF/C#:實現導航功能C#
- 前端黑科技:使用 JavaScript 實現網頁掃碼功能前端JavaScript網頁
- C# 截圖並儲存為圖片C#
- 如何用Chrome自帶的截圖功能擷取超過一個螢幕的網頁Chrome網頁