用如下的代码 进行 网页截图
public class GetSnap
{
private string MyURL;
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
} public GetSnap(string WebSite/*, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight*/)
{
this.WebSite = WebSite;
}
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite/*, this.ScreenWidth, this.ScreenHeight*/);
Shot.GetIt();
//Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
Bitmap Pic = Shot.DrawBitmap();
return Pic;
}
}
class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url/*, int width, int height*/)
{
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
}
public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
} this.Height = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollHeight"));
this.Width = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollwidth"));
MyBrowser.Size = new Size(this.Width, this.Height);
}
public Bitmap DrawBitmap(/*int theight, int twidth*/)
{
int theight=this.Height;
int twidth = this.Width;
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;
}
}
}
截图页 default.aspx.cs
添加button1事件: protected void Button1_Click(object sender, EventArgs e)
{ Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
NewTh.Start(); }
///// 捕获屏幕
public void CaptureImage()
{
//你的任务代码
try
{
string url=http://www.baidu.com;
GetSnap thumb = new GetSnap(url);
System.Drawing.Bitmap x = thumb.GetBitmap();//获取截图
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(Server.MapPath("SnapPic/" + FileName + ".jpg"));//保存截图到SnapPic目录下
}
catch (Exception ex)
{
}
}点击button 后,可以对指定的 url的网页进行截图,现在给出 (x1,y1) (x2,y2) 两个坐标值 ,为矩形的对角线的端点的坐标,单位为像素 ,要求在这两个点所确定的矩形框上画红线,在上面的代码在哪里改,怎么改
public class GetSnap
{
private string MyURL;
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
} public GetSnap(string WebSite/*, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight*/)
{
this.WebSite = WebSite;
}
public Bitmap GetBitmap()
{
WebPageBitmap Shot = new WebPageBitmap(this.WebSite/*, this.ScreenWidth, this.ScreenHeight*/);
Shot.GetIt();
//Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
Bitmap Pic = Shot.DrawBitmap();
return Pic;
}
}
class WebPageBitmap
{
WebBrowser MyBrowser;
string URL;
int Height;
int Width;
public WebPageBitmap(string url/*, int width, int height*/)
{
this.URL = url;
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
}
public void GetIt()
{
MyBrowser.Navigate(this.URL);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
} this.Height = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollHeight"));
this.Width = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollwidth"));
MyBrowser.Size = new Size(this.Width, this.Height);
}
public Bitmap DrawBitmap(/*int theight, int twidth*/)
{
int theight=this.Height;
int twidth = this.Width;
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;
}
}
}
截图页 default.aspx.cs
添加button1事件: protected void Button1_Click(object sender, EventArgs e)
{ Thread NewTh = new Thread(CaptureImage);
NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
NewTh.Start(); }
///// 捕获屏幕
public void CaptureImage()
{
//你的任务代码
try
{
string url=http://www.baidu.com;
GetSnap thumb = new GetSnap(url);
System.Drawing.Bitmap x = thumb.GetBitmap();//获取截图
string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
x.Save(Server.MapPath("SnapPic/" + FileName + ".jpg"));//保存截图到SnapPic目录下
}
catch (Exception ex)
{
}
}点击button 后,可以对指定的 url的网页进行截图,现在给出 (x1,y1) (x2,y2) 两个坐标值 ,为矩形的对角线的端点的坐标,单位为像素 ,要求在这两个点所确定的矩形框上画红线,在上面的代码在哪里改,怎么改
g.DrawRectangle(…);//加上
try { return (Bitmap)oThumbNail; } catch (Exception ex) {