对指定的网页进行截图的效果 C#版
程序员文章站
2023-11-14 14:13:46
碰到一个项目,需要对指定的网页进行截图保存,晕死! 需求永远都是怪异的..... 解决是关键~ 遂写了以下代码,快准狠!(因为赶时间!) 可以实现对指定的页面获取,按指定的...
碰到一个项目,需要对指定的网页进行截图保存,晕死!
需求永远都是怪异的.....
解决是关键~
遂写了以下代码,快准狠!(因为赶时间!)
可以实现对指定的页面获取,按指定的大小生成缩略图,当然也可以1:1的产生图,
页面上的javascript 运行对截图貌似没任何影响,相当的正常,我个人都觉得很神奇。
首先对项目添加系统引用
system.drawing;
system.drawing.design;
system.windows.forms;
获取指定网页并转换成图片的类:
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;
}
}
}
}
以下是调用方法,懒省事的方法,嘿嘿,赶时间就不说什么了,反正上面的抓取转换类已经写出来了,大家尽情的用异步,线程等方法自己玩吧!~
string urlpath;
bool capturestate = false;
guid guid;
protected bool saveoriginalpagetoimage(guid myguid)
{
//使用guid 来命名
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)
{
}
//返回截取状态
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;
}
}
需求永远都是怪异的.....
解决是关键~
遂写了以下代码,快准狠!(因为赶时间!)
可以实现对指定的页面获取,按指定的大小生成缩略图,当然也可以1:1的产生图,
页面上的javascript 运行对截图貌似没任何影响,相当的正常,我个人都觉得很神奇。
首先对项目添加系统引用
system.drawing;
system.drawing.design;
system.windows.forms;
获取指定网页并转换成图片的类:
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;
}
}
}
}
以下是调用方法,懒省事的方法,嘿嘿,赶时间就不说什么了,反正上面的抓取转换类已经写出来了,大家尽情的用异步,线程等方法自己玩吧!~
string urlpath;
bool capturestate = false;
guid guid;
protected bool saveoriginalpagetoimage(guid myguid)
{
//使用guid 来命名
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)
{
}
//返回截取状态
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;
}
}
上一篇: Linux基础知识