C# 网页截图

24 篇文章 0 订阅
20 篇文章 0 订阅

许多人都在想如何进行网页截图,其实简单的方法是利用WebBrowser进行截图,下面提供一个截图类GetSnap,可以在web和winform上进行截图。

 

类GetSnap:

======================================

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
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;

/// <summary>
/// GetSnap 的摘要说明
/// </summary>

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;
        }
   }
}
===========================================

在web中截图:

新建test.aspx页面,引入命名空间using System.Drawing.Imaging;using System.Threading;

 

添加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)
        {
        }
    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值