手机
当前位置:查字典教程网 >编程开发 >C#教程 >对指定的网页进行截图的效果 C#版
对指定的网页进行截图的效果 C#版
摘要:碰到一个项目,需要对指定的网页进行截图保存,晕死!需求永远都是怪异的.....解决是关键~遂写了以下代码,快准狠!(因为赶时间!)可以实现对...

碰到一个项目,需要对指定的网页进行截图保存,晕死!

需求永远都是怪异的.....

解决是关键~

遂写了以下代码,快准狠!(因为赶时间!)

可以实现对指定的页面获取,按指定的大小生成缩略图,当然也可以1:1的产生图,

页面上的javascript运行对截图貌似没任何影响,相当的正常,我个人都觉得很神奇。

首先对项目添加系统引用

System.Drawing;

System.Drawing.Design;

System.Windows.Forms;

获取指定网页并转换成图片的类:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

usingSystem.Drawing.Imaging;

usingSystem.Windows.Forms;

usingSystem.Diagnostics;

namespaceMyLib

{

publicclassGetImage

{

privateintS_Height;

privateintS_Width;

privateintF_Height;

privateintF_Width;

privatestringMyURL;

publicintScreenHeight

{

get{returnS_Height;}

set{S_Height=value;}

}

publicintScreenWidth

{

get{returnS_Width;}

set{S_Width=value;}

}

publicintImageHeight

{

get{returnF_Height;}

set{F_Height=value;}

}

publicintImageWidth

{

get{returnF_Width;}

set{F_Width=value;}

}

publicstringWebSite

{

get{returnMyURL;}

set{MyURL=value;}

}

publicGetImage(stringWebSite,intScreenWidth,intScreenHeight,intImageWidth,intImageHeight)

{

this.WebSite=WebSite;

this.ScreenWidth=ScreenWidth;

this.ScreenHeight=ScreenHeight;

this.ImageHeight=ImageHeight;

this.ImageWidth=ImageWidth;

}

publicBitmapGetBitmap()

{

WebPageBitmapShot=newWebPageBitmap(this.WebSite,this.ScreenWidth,this.ScreenHeight);

Shot.GetIt();

BitmapPic=Shot.DrawBitmap(this.ImageHeight,this.ImageWidth);

returnPic;

}

}

classWebPageBitmap

{

WebBrowserMyBrowser;

stringURL;

intHeight;

intWidth;

publicWebPageBitmap(stringurl,intwidth,intheight)

{

this.Height=height;

this.Width=width;

this.URL=url;

MyBrowser=newWebBrowser();

MyBrowser.ScrollBarsEnabled=false;

MyBrowser.Size=newSize(this.Width,this.Height);

}

publicvoidGetIt()

{

MyBrowser.Navigate(this.URL);

while(MyBrowser.ReadyState!=WebBrowserReadyState.Complete)

{

Application.DoEvents();

}

}

publicBitmapDrawBitmap(inttheight,inttwidth)

{

BitmapmyBitmap=newBitmap(Width,Height);

RectangleDrawRect=newRectangle(0,0,Width,Height);

MyBrowser.DrawToBitmap(myBitmap,DrawRect);

System.Drawing.ImageimgOutput=myBitmap;

System.Drawing.ImageoThumbNail=newBitmap(twidth,theight,imgOutput.PixelFormat);

Graphicsg=Graphics.FromImage(oThumbNail);

g.CompositingQuality=CompositingQuality.HighSpeed;

g.SmoothingMode=SmoothingMode.HighSpeed;

g.InterpolationMode=InterpolationMode.HighQualityBilinear;

RectangleoRectangle=newRectangle(0,0,twidth,theight);

g.DrawImage(imgOutput,oRectangle);

try

{

return(Bitmap)oThumbNail;

}

catch(Exceptionex)

{

returnnull;

}

finally

{

imgOutput.Dispose();

imgOutput=null;

MyBrowser.Dispose();

MyBrowser=null;

}

}

}

}

以下是调用方法,懒省事的方法,嘿嘿,赶时间就不说什么了,反正上面的抓取转换类已经写出来了,大家尽情的用异步,线程等方法自己玩吧!~

stringUrlPath;

boolCaptureState=false;

Guidguid;

protectedboolSaveOriginalPageToImage(GuidmyGuid)

{

//使用guid来命名

guid=myGuid;

if(this.CurrentPageAct==PageAct.Edit)

{

stringPagePath=Request.Url.LocalPath;

PagePath=PagePath.Replace("Operation","Capture");

UrlPath=PagePath+"?act=view&ProjectNo="+_projectNo;

ThreadNewTh=newThread(CaptureImage);

NewTh.SetApartmentState(ApartmentState.STA);

NewTh.Start();

while(NewTh.ThreadState==ThreadState.Running)

{

}

//返回截取状态

returnCaptureState;

}

returnfalse;

}

/**////<summary>

///捕获屏幕

///</summary>

///<paramname="UrlPath"></param>

///<returns></returns>

publicvoidCaptureImage()

{

try

{

stringurl="http://"+Request.Url.Host+":"+Request.Url.Port.ToString();

url=url+UrlPath;

GetImagethumb=newGetImage(url,1024,1200,1024,1200);

System.Drawing.Bitmapx=thumb.GetBitmap();

stringFileName=DateTime.Now.ToString("yyyyMMddhhmmss");

x.Save(Server.MapPath("~/Capture/SavePage")+""+guid+".jpg");

CaptureState=true;

}

catch(Exceptionex)

{

CaptureState=false;

}

}

【对指定的网页进行截图的效果 C#版】相关文章:

ref 和out传参的区别分析

C# L型棋牌覆盖实现代码与效果

C#几种获取网页源文件代码的实例

验证本机的excel版本的C#代码

C#对XML文件的各种操作实现方法

C#定位txt指定行的方法小例子

C#中判断本地系统的网络连接状态的方法

c#开发的程序安装时动态指定windows服务名称

用C#对ADO.NET数据库完成简单操作的方法

将数组中指定数量的元素移动数组后面的实现代码

精品推荐
分类导航