對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖的效果 C#版
更新時(shí)間:2007年08月29日 19:36:15 作者:
碰到一個(gè)項(xiàng)目,需要對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖保存,暈死!
需求永遠(yuǎn)都是怪異的.....
解決是關(guān)鍵~
遂寫(xiě)了以下代碼,快準(zhǔn)狠!(因?yàn)橼s時(shí)間?。?
可以實(shí)現(xiàn)對(duì)指定的頁(yè)面獲取,按指定的大小生成縮略圖,當(dāng)然也可以1:1的產(chǎn)生圖,
頁(yè)面上的javascript 運(yùn)行對(duì)截圖貌似沒(méi)任何影響,相當(dāng)?shù)恼?,我個(gè)人都覺(jué)得很神奇。
首先對(duì)項(xiàng)目添加系統(tǒng)引用
System.Drawing;
System.Drawing.Design;
System.Windows.Forms;
獲取指定網(wǎng)頁(yè)并轉(zhuǎn)換成圖片的類:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
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 oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = 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;
}
}
}
}
以下是調(diào)用方法,懶省事的方法,嘿嘿,趕時(shí)間就不說(shuō)什么了,反正上面的抓取轉(zhuǎn)換類已經(jīng)寫(xiě)出來(lái)了,大家盡情的用異步,線程等方法自己玩吧!~
string UrlPath;
bool CaptureState = false;
Guid guid;
protected bool SaveOriginalPageToImage(Guid myGuid)
{
//使用guid 來(lái)命名
guid = myGuid;
if (this.CurrentPageAct == PageAct.Edit)
{
string PagePath = Request.Url.LocalPath;
PagePath = PagePath.Replace("Operation", "Capture");
UrlPath = PagePath + "?act=view&ProjectNo=" + _projectNo;
Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);
NewTh.Start();
while (NewTh.ThreadState == ThreadState.Running)
{
}
//返回截取狀態(tài)
return CaptureState;
}
return false;
}
/**//// <summary>
/// 捕獲屏幕
/// </summary>
/// <param name="UrlPath"></param>
/// <returns></returns>
public void CaptureImage()
{
try
{
string url = "http://" + Request.Url.Host + ":" + Request.Url.Port.ToString();
url = url + UrlPath;
GetImage thumb = new GetImage(url, 1024, 1200, 1024, 1200);
System.Drawing.Bitmap x = thumb.GetBitmap();
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(Server.MapPath("~/Capture/SavePage") + "\\" + guid + ".jpg");
CaptureState = true;
}
catch (Exception ex)
{
CaptureState = false;
}
}
需求永遠(yuǎn)都是怪異的.....
解決是關(guān)鍵~
遂寫(xiě)了以下代碼,快準(zhǔn)狠!(因?yàn)橼s時(shí)間?。?
可以實(shí)現(xiàn)對(duì)指定的頁(yè)面獲取,按指定的大小生成縮略圖,當(dāng)然也可以1:1的產(chǎn)生圖,
頁(yè)面上的javascript 運(yùn)行對(duì)截圖貌似沒(méi)任何影響,相當(dāng)?shù)恼?,我個(gè)人都覺(jué)得很神奇。
首先對(duì)項(xiàng)目添加系統(tǒng)引用
System.Drawing;
System.Drawing.Design;
System.Windows.Forms;
獲取指定網(wǎng)頁(yè)并轉(zhuǎn)換成圖片的類:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
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 oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
Graphics g = Graphics.FromImage(oThumbNail);
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = 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;
}
}
}
}
以下是調(diào)用方法,懶省事的方法,嘿嘿,趕時(shí)間就不說(shuō)什么了,反正上面的抓取轉(zhuǎn)換類已經(jīng)寫(xiě)出來(lái)了,大家盡情的用異步,線程等方法自己玩吧!~
string UrlPath;
bool CaptureState = false;
Guid guid;
protected bool SaveOriginalPageToImage(Guid myGuid)
{
//使用guid 來(lái)命名
guid = myGuid;
if (this.CurrentPageAct == PageAct.Edit)
{
string PagePath = Request.Url.LocalPath;
PagePath = PagePath.Replace("Operation", "Capture");
UrlPath = PagePath + "?act=view&ProjectNo=" + _projectNo;
Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);
NewTh.Start();
while (NewTh.ThreadState == ThreadState.Running)
{
}
//返回截取狀態(tài)
return CaptureState;
}
return false;
}
/**//// <summary>
/// 捕獲屏幕
/// </summary>
/// <param name="UrlPath"></param>
/// <returns></returns>
public void CaptureImage()
{
try
{
string url = "http://" + Request.Url.Host + ":" + Request.Url.Port.ToString();
url = url + UrlPath;
GetImage thumb = new GetImage(url, 1024, 1200, 1024, 1200);
System.Drawing.Bitmap x = thumb.GetBitmap();
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(Server.MapPath("~/Capture/SavePage") + "\\" + guid + ".jpg");
CaptureState = true;
}
catch (Exception ex)
{
CaptureState = false;
}
}
您可能感興趣的文章:
- 解決C# 截取當(dāng)前程序窗口指定位置截圖的實(shí)現(xiàn)方法
- c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
- C#截圖程序類似騰訊QQ截圖實(shí)現(xiàn)代碼
- 解決C#全屏幕截圖的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能
- C#實(shí)現(xiàn)屬于自己的QQ截圖工具
- c# 控件截圖的簡(jiǎn)單實(shí)例
- C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法
- C#中的FileUpload 選擇后的預(yù)覽效果具體實(shí)現(xiàn)
- C# DirectShow預(yù)覽攝像頭并截圖
相關(guān)文章
C#實(shí)現(xiàn)常見(jiàn)加密算法的示例代碼
這篇文章主要為大家詳細(xì)介紹一下C#中一些常見(jiàn)加密算法(Base64編碼、凱撒密碼、Vigenere密碼、DES、AES)以及它們的實(shí)現(xiàn)代碼,感興趣的可以了解一下2022-07-07
c#數(shù)據(jù)綁定之向查詢中添加參數(shù)(.Net連接外部數(shù)據(jù)庫(kù))
本實(shí)例主要練習(xí)了ADO.Net連接到外部數(shù)據(jù)庫(kù)的基礎(chǔ)上,向查詢中添加參數(shù)。使用的是ACCESS數(shù)據(jù)庫(kù)2014-04-04
Unity實(shí)現(xiàn)卡牌翻動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)卡牌翻動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
淺談C#2.0泛型中的變化:default關(guān)鍵字
下面就詳細(xì)的說(shuō)明一下。之所以會(huì)用到default關(guān)鍵字,是因?yàn)樾枰诓恢李愋蛥?shù)為值類型還是引用類型的情況下,為對(duì)象實(shí)例賦初值2013-09-09
C#實(shí)現(xiàn)過(guò)濾sql特殊字符的方法集合
這篇文章主要介紹了C#實(shí)現(xiàn)過(guò)濾sql特殊字符的方法,以實(shí)例形式分析總結(jié)了C#針對(duì)SQL危險(xiǎn)字符的幾種常用的過(guò)濾技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-11-11

