碰到一个项目,需要对指定的网页进行截图保存,晕死!
需求永远都是怪异的.....
解决是关键~
遂写了以下代码,快准狠!(因为赶时间!)
可以实现对指定的页面获取,按指定的大小生成缩略图,当然也可以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;
}
}