网站截图的原理——从服务器端模拟客户端

很多人可能奇怪,网站截图?那是发生在客户端上的啊,而我们所运行的ASP.net程序是运行在服务器端的,这是两台不同的机器啊,怎么可能会截图呢?当然,截图的方式方法很多,最普片的是通过ActiveX在客户端执行,但是,如果除去ActiveX,我们同样可以实现——通过服务器模拟方式。

模拟客户端,也就是说,在服务器端内存中生成一个浏览器对象,通过该虚拟的浏览器访问某个网站,再通过GDI对这个虚拟的浏览器截图。这也就是整个网站截图的思路和原理。

        WebBrowser MyBrowser; //声明浏览器对象

         public  WebPageBitmap( string url,  int width,  int height)
        {
            ... ...
             //创建浏览器对象

            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( this.Width,  this.Height);
            Rectangle DrawRect =  new Rectangle(0, 0,  this.Width,  this.Height);
            MyBrowser.DrawToBitmap(myBitmap, DrawRect);
            System.Drawing.Image imgOutput = myBitmap;
            System.Drawing.Bitmap oThumbNail =  new Bitmap(twidth, theight, imgOutput.PixelFormat);
            Graphics g = Graphics.FromImage(oThumbNail);

            ... ...
        }

最后一步,则是使用GDI技术绘制,在这里,我们通过DrawToBitmap方法,直接将浏览器的图像绘制到图像中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值