對(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#連接SQL數(shù)據(jù)庫(kù)和查詢數(shù)據(jù)功能的操作技巧
本文給大家分享C#連接SQL數(shù)據(jù)庫(kù)和查詢數(shù)據(jù)功能的操作技巧,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-05-05解析C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn)
這篇文章主要介紹了C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn),分別舉了類的對(duì)象適配器與對(duì)象的適配器模式的例子,需要的朋友可以參考下2016-02-02C#中while循環(huán)語(yǔ)句用法實(shí)例詳解
這篇文章主要介紹了C#中while循環(huán)語(yǔ)句用法,以實(shí)例形式詳細(xì)分析了while語(yǔ)句的用法,并對(duì)return,continue,break的區(qū)別做了進(jìn)一步的分析,需要的朋友可以參考下2014-10-10C#面向?qū)ο笾M實(shí)現(xiàn)商城購(gòu)物功能
這篇文章主要為大家詳細(xì)介紹了C#面向?qū)ο笾M實(shí)現(xiàn)商城購(gòu)物功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C# Access數(shù)據(jù)庫(kù)增刪查改的簡(jiǎn)單方法
這篇文章主要介紹了C# Access數(shù)據(jù)庫(kù)增刪查改的簡(jiǎn)單方法,有需要的朋友可以參考一下2014-01-01Unity中Instantiate實(shí)例化物體卡頓問(wèn)題的解決
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)離線計(jì)時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10C# 指針內(nèi)存控制Marshal內(nèi)存數(shù)據(jù)存儲(chǔ)原理分析
這篇文章主要介紹了C# 指針 內(nèi)存控制 Marshal 內(nèi)存數(shù)據(jù)存儲(chǔ)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02